C++ - SPLessons

C++ Web Programming

Chapter 38

SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

C++ Web Programming

C++ Web Programming

shape Introduction

CPP Web Programming chapter gives the clear description of CGI interface and HTTP Protocol. Any technology’s final destination will be the user. Writing programs is not enough, all those must be placed in server as an application which will be used by the end-users. Also, there must be an interface following all the instructions given by the developer. CGI is one that interface which helps to connect the user with the server.  

shape Description

The interface having a protocol that makes the code to communicate with the web server and the end-users is known as “Common Gateway Interface“. CGI performs input and output operations dynamically by interacting with clients, data bases and web servers.

HTTP protocol

shape Mechanism

HTTP acts as a protocol between the client and the server.

  • When a hyperlink is clicked to access a webpage, browser interacts the HTTP web server and asks the URL.If it is found in the database, the URL is sent back to the server otherwise an error is sent to the server.
  • Web browser receives the response from the serves and displays the result on the screen.

shape Conceptual

Configuration of web server

shape Description

Along with the C++ compiler, one need to download “Apache HTTP server” and install the binary file and configure.Now check if service is running using url http://loaclhost.

Till now, cout has given standard output on the console screen.But now the output can be seen on the browser page.So, while writing cout statement be careful.

The code till now is saved with an extension .exe.To send the code onto the server the file has to be stored with .cgi extension.
Eg: “http://localhost/cgi-bin/firstprogram.exe” is changed to “http://localhost/cgi-bin/firstprogram.cgi”

CGI program

shape Example

Let’s write a simple CGI program.

#include <iostream>
#include <iostream>
#include <ctime>
using namespace std;

int main()
	time_t now;
	cout << "Content-Type: text/html\n\n";

	cout << "<?xml version = \"1.0\"?>"
	<< "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.1//EN\" "
	<< "\"[url="http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd\%22>%22;"]http://www.w3.org/TR...html11.dtd\">";[/url]

	cout << "<html xmlns = \"[url="http://www.w3.org/1999/xhtml\%22>"]http://www.w3.org/19.../1999/xhtml\">"[/url]
	<< "<head><title>cgi program</title></head>"
	<< "<body>"
	<< "<h1>Welcome to splessons</h1>"
	<< "</body></html>";
	return 0;

The above program has to be compiled and copy the .exe file to cgi-bin directory.
Now restart the web server and give the url with file name.


shape Key Points

CPP Web Programming chapter draws out following important points.

  • Interface to communicate between end-users and the server is possible by CGI.
  • HTTP protocol acts as a mediator.
  • Apache HTTP Server is used to run web applications.