There are good web servers both in the free/open source arena as well as commercial products. Apache and Lighttp are widely used in the UN*X based systems. PHP, Python based Django or Ruby on Rails usually run on top of Apache or Lighttp. However if you wish to install a small web server on a device so that it can be accessed by any small device such as a smart phone or tablet then you need a web server that has a small footprint and that is fast.
The main advantage of HTTP/HTML is that every client device has a web browser installed and hence can access a web server without a need to install additional software. ‘Internet of Things’ (IoT) is now becoming popular and a web server on the device is one possible solution. If there are a large number of devices then a central server would be better.
The second reason for writing a web server is pedagogic. The established web severs provide so many features that one often forgets what is actually happening. Here I will discuss how to install as web sever using Asio
Asio is a header only C++ software module that helps in writing asynchronous socket IO. It was originally part of the Boost library. Asio has now been ported it to using just standard C++11. Asio contains a sample http server. In this blog I’ll describe to build and run this server using Visual C++ 2015. Baker has used Asio to create a http server library that is simpler to use although the master branch uses Boost.
The first step is to install a C++ compiler that is C++11 standards compliant. GCC 4.9 and Visual C++ 14 are both by and large C++11 compliant and to a lesser extent C++14 standards compliant. I have used only those two and there is little difference between the two as far as this task is concerned.
The next task is to download the Asio library. Using Git pull the master branch of Asio. You can also download the zip file from the website. But it is better to invest time learning to use Git if you haven’t already.
There are a number of single file examples in Asio. On Windows use the Visual Studio Native Tools command prompt (64-bit or 32-bit) and set the current working folder (‘cd’) to ‘asio\src’. Then run this compiler command:
cl -I..\include -DASIO_STANDALONE -MT -D_WIN32_WINNT=0x0603 /EHsc examples\cpp11\chat\chat_client.cpp
- -DASIO_STANDALONE: prompts Asio not to look for Boost
- -MT: specific to VC++ compiler;use multi-threaded static versions of the library
- /EHsc: specific to VC++ compiler; enable exception handling.
- -D_WIN32_WINNT=0x0603: telling Asio the lowest version of Windows system software to use.
Visual Studio can also be used to produce the executables. Just create an empty console application project and add the file making sure to set the pre-processor definitions mentioned above.
Building the sample server in Visual Studio
A sample http server is available in the folder: asio\src\examples\cpp11\http\server. Again just create an empty console application project and add all the files in the folder making sure to set the pre-processor definitions mentioned above. Also ensure that include folders include asio.hpp. The sample server will use the folder mentioned as a command line argument to be the wwwroot folder.
Camerasp is a web server that runs on Raspberry Pi and uses via-http and raspicam to serve camera captured images. It is a simple project that demonstrates the use of the libraries described here.