C++ Web Programming

In this C++ tutorial, you will get a new practice of linking web with your C++ programs. Let us see about web programming in C++ with suitable examples.

Introduction of CGI

Common Gateway Interface(CGI) is a set of standards defining how the data is exchanged from the web server, how it is passing the web user’s request to an application program and to receive data back to the user. When any user requests for a web page, then the server returns the requested page.

This method or protocol for passing data back and forth between the server and the application is called the Common Gateway Interface (CGI) that is also a part of the Web’s Hypertext Transfer Protocol (HTTP).

Web Browsing

To understand the concept of CGI, let us see what happens when we click a hyperlink to browse a website through the internet.

  • Your browser contacts the HTTP web server and demand for the URL ie. filename.
  • Web Server will parse the URL and will look for the filename. If it finds requested file, then web server returns that file to the browser. Otherwise, it sends an error message.
  • Web browser takes the response from a web server and displays either the received file or error message based on the received response.

Server Configuration

Before using CGI programming, the coders should make ensure the Web server supports CGI and is well configured for handling CGI programs. By custom, CGI files will have an extension as .cgi, though they are C++ executable. By default, Apache Web Server is configured to run CGI programs in /var/www/cgi-bin. 

Program

#include <iostream>
void main ()
{
   cout << "Content-type:text/html\r\n\r\n";
    cout << "<html>\n";
     cout << "<head>\n";
     cout << "<title>Hello TutorialsCloud </title>\n";
     cout << "</head>\n";
     cout << "<body>\n";
    cout << "<h3> <b> First CGI program </b> </h2>\n";
    cout << "</body>\n";
   cout << "</html>\n";
}

Output

Content-type:text/html
<html>
<head>
<title>Hello TutorialsCloud </title>
</head>
<body>
<h3> <b> First CGI program </b> </h2>
</body>
</html>

Leave a Comment

Your email address will not be published. Required fields are marked *