

Dheeraj Jha
Jun 29, 20205 min read


Dheeraj Jha
May 3, 20204 min read


Dheeraj Jha
Apr 25, 20204 min read


Dheeraj Jha
Mar 13, 20202 min read


Dheeraj Jha
Mar 4, 20203 min read

Updated: May 1, 2020
Source files are here.
As the title suggests, in this article we are going to develop a simple Web Server in C++.
This article only explains the library I am using to develop a web server and a simple “Namaste World!” web server. But I plan to extend this and develop a REST server which will fulfil some purpose and all APIs will fulfil REST Arch principle.
Before jumping into the code lets understand what is a web server and HTTP in brief.
As per Wikipedia.org
A web server is server software or hardware dedicated to running this software, that can satisfy client requests on the World Wide Web. A web server can, in general, contain one or more websites. A web server processes incoming network requests over HTTP and several other related protocols.

Let me brief this considering it as the simplest webserver possible. A web client/browser makes a request to a web server for some data or any file to process and show to the user. When this request from client reaches to correct web server, web server accepts this request and process this request. It checks for the data requested by clients. To get the correct data, this webserver further may request to any FTP, APP or DB server. Once the webserver gets correct data, it sends a response to web client/browser with requested data. These FTP, App or DB server may be on the same hardware where the webserver is running or maybe on different hardware. Where these servers will be located, totally depends on the requirement. Both approaches have their pros and cons.
Wikipedia says "The Hypertext Transfer Protocol (HTTP) is an application protocol for distributed, collaborative, hypermedia information systems. HTTP is the foundation of data communication for the World Wide Web, where hypertext documents include hyperlinks to other resources that the user can easily access, for example by a mouse click or by tapping the screen in a web browser." I will not go in detail of HTTP protocol in this blog. Maybe in a future blog, I will discuss more.
Now, let's start writing our first HTTP web server.
To create our web server, I am going to use cpprest/Casablanca SDK provided by Microsoft. “The C++ REST SDK is a Microsoft project for cloud-based client-server communication in native code using a modern asynchronous C++ API design. This project aims to help C++ developers connect to and interact with services.” - this description is from their official GitHub page. I hope it is pretty clear so need not to give my commentary here.
Let's get back to our server. server.cpp: is the file which will start our server.
We have a main() in this file., which will start our server. Here we will create an address for our server where our server is running. This address contains 2 parts: IP address and port. This application takes a port number as a command-line parameter and if nothing is passed it takes 34568 as a default port.
utility::string_t address = U("http://localhost:");
address.append(port);Our server is running on the same machine and our client is also on the same machine to test so I am using localhost. If client and server are is running on some different machine, you can provide IP of your server machine here.
Next have a call to
on_initialize(address);This function will initialize our server e.g in our case it will create a handler object which will handle all the incoming request.
In this function, first, we have to create a URI(Uniform Resource Identifier). This URI we will use to build our handler object which will listen and accept all the requests with this URI.
uri_builder uri(address);uri_builder is a class defined in ‘uri_builder.h’.
Now, let's convert it into a string and pass this to our Handler class. This Handler class is responsible for accepting all the request and sending a response.
g_httpHandler = std::unique_ptr<Handler>(new Handler(addr));now g_httpHandler is pointing to a Handler class object. We call a function of this class Handler::open().
Inside this http_listener::open() function of is called. This function “asynchronously open the listener, i.e. start accepting requests.” It will return a task that will be completed once this listener is opened, accepting requests.
Wait() is a function defined in pplxtasks.h which waits for this task to reach a terminal state.
We also have
void on_shutdown(){
g_httpHandler->close().wait();
return;
}This function gets called when we want to close over the server. This is the correct place to release all the resources and close http_listener.
handler.h/handler.cpp: has a Handler class which will serve all the request.
For the simplicity of this program, I am handling only GET request. In the same way, you can handle POST, PUT and other requests. I am only sending a simple response to other methods.
In Handler constructor, we are calling support() function.
m_listener.support(methods::GET, std::bind(&Handler::handle_get, this, std::placeholders::_1));If we see in the definition of support function, it maintains a map which stores a mapping of method and handler.
std::map<http::method, std::function<void(http::http_request)>> m_supported_methods;std::bind() is a standard c++ function which generates a forwarding call wrapper.
Now Handler::handle_get() is the handler function which will serve any get request coming to this server.
void Handler::handle_get(http_request message)In this function, I am not doing anything special. Just sending a response in two different conditions.
I am leaving this part for readers to explore. Let me know if you have any queries.
http::uri::split_path()split_path() function returns a std::vector<utility::string_t> which contains strings in relative address separated by “/”.
To test this server, I used Postman as a client.

So thanks for reading to this point. This is it for this blog. I plan to extend this server and connect to some NoSQL DB(e.g. MongoDB) and run it inside a container and host it on cloud blah blah blah... I will keep posting in my blogs what I am doing. Till then stay tuned and keep sharing this post with others.
Good reading:










Thanks, Mehul. You liked this post. As per my knowledge, there will always a demand for c++. It will not go out, at least for the next 10-15 years(who knows the future). About cloud-based applications, I think people less prefer c++ as a client-side technology but it is widely used in server-side. In fact, in all the application which requires high performances, people prefer to use c++.
However its always good to know multiple languages, it will help you to grow in the long run. Hope I answered you.
Very nice blog, it's very unique, Dheeraj I want to know is there any demand for the c++ developer for developing cloud based applications , I want to know the scope of c++ technology, from past one year I am working as a c++ dev, but still I am unaware of its scope