In this article, we will learn about web servers in C++. The main purpose of the web server is to handle the arriving HTTP requests, process all the coming requests, and respond with the web HTML content in return. We need to maintain the web server in C++ by just adding the networking libraries in the C++ environment, like “sockets”, to handle the networking communications and issues. A web server is the backbone of the internet and is a way of communication between users and main servers. Let’s evaluate the process of creating a simple web server through proper example implementations.
Setting Up the Environment
We first need to set up all the required options in our environment. We need to utilize the sockets to listen to the incoming HTTP requests. After that, we bind the socket to the specific port on the host machine as our system. After that, when the clients call for the request, the server listens to these incoming connected requests. The server responds to the user`s requests through HTTP requests to fetch the user’s request to the server. After that, the server handles the request and returns the HTTP responses to the user with proper crafting.
Sockets Handling of the Web Server
In this session, we will learn that sockets create communication between different processes that run in our system. The sockets are basically used to create the communication or connection between the client`s browser and our server. The connection of the socket is built in C++ in the way that is mentioned in the following:
Here, we will define the libraries for sockets as “#include <sys/socket.h>”. In the main function, we create the socket by initializing the “server_fd”. After that, we check the value of the “server_fd” that contains the stock connection validation. If the server is down and not working fine, it returns or displays the “Socket creation failed” message. Otherwise, the message that has the IP address of the web server will show the data of HTML on the web server.
#include <sys/socket.h>
#include <netinet/in.h>
int main() {
int server_fd = socket(AF_INET, SOCK_STREAM, 0);
if (server_fd == -1) {
std::cerr << "Socket creation failed";
return -1;
}
// else
// {
// std::cout<<"exit";
// }
// bind
std::cout<<"http//:127.0.0.1:8080";
// Further code goes here
return 0;
}
The output of this code is attached in the following:
This shows that the socket is created successfully in C++ to connect with the web server.
Web Server Handling in C++
The system’s web server is set up to handle the different C++ concepts like merge sorting, linear sorting, etc. Remember to initialize the important networking libraries. The web server runs on the local machine (127.0.0.1) and listens on port 8080.
Include the necessary libraries in the C++ programming language as follows:
Remember that creating a web server in C++ is not an easy and time-consuming process. We can handle the incoming connections using “async_accept”.
Server Responses in the C++ Code on the Web Server
The server response must be HTTP which works on the browser and shows the output on the HTML view on the web page. All the libraries manage the servers and handle all valid requests.
Binding and Listening of the Web Server
Next, we need to know about the binding and listening of the web server to execute the code on the web page easily. After that, we need to bind the socket to the given address of our system, basically the IP address and port on which these sockets are active. On the port, we start listening for incoming connections.
We need to initialize the few libraries to bind and link.
We can bind the server. If the server is down or not connected, the message is not displayed on the web page of any browser.
Accepting the Connections to the Web Server
In this section, we will learn how we can control the flow of the client`s requests. The server accepts all client requests through the HTTP protocol. The server socket indicates the size of the message that is received on the web server from the client. The message contains the server socket connection and address more clearly and uniquely. We check if the server needs to respond if the user socket is found or not. If the user socket connection is not found, we cannot accept the connection to a web server.
Methods of Sending and Receiving the Data from the User and Server End
After creating the socket links and accepting the connection, the next thing is to send the data to the server side and receive the data from the server using different keywords and building the functions that are related to sending and receiving the data in C++. Here, we check the response of the data. Is this request to get the data or to POST the data from the web server? We check the response status of the client’s request. We temporarily store all the client requests on the buffer to display the message or output on the web server.
Linear Sorting in C++
Let’s perform the linear sorting here and display the result of linear sorting on the web server easily. The code snippet of the linear sorting is attached in the following:
using namespace std;
void LinearSearch(int arr[], int len, int item){
for(int i=0;i<len;i++){
if(arr[i] == item){
cout << item << " Found at index : " << i;
return;
}
}
cout << "Not found";
}
int main() {
int arr[] = {10, 5, 15, 21, -3, 7};
int len = sizeof(arr)/sizeof(arr[0]);
int item = 21;
LinearSearch(arr, len, item);
return 0;
}
In this example, we search the item “21” at which index. So, we run this code. The output of this code is attached in the following:
This output is now open on the web server like in any browser such as Chrome, Edge, etc.
Terminate All the Connections After the Output Display
This is the last step in handling the data on a web server. Remember to close the socket connections after performing the required output. We close the socket here using the functions such as “close(server_id)” and “close (client socket)”.
At the end of running the server, the expected output is displayed on the web page by accessing the address as “http//localhost:8080”.
Conclusion
At the end of this article, we can say that the simple web server handling in C++ is really complex and needs full concentration to send and receive the HTTP client`s request. We can expand the web server functionality to display the content dynamically on the web page. Hopefully, this article is helpful for you, and you can run these examples on your environment to understand the steps that are required to create a simple web server in C++.