Click here to Skip to main content
15,886,799 members
Articles / Web Development / HTML

A C++ Embedded Web Server

Rate me:
Please Sign up or sign in to vote.
4.76/5 (47 votes)
23 Jun 2014BSD7 min read 449.4K   8.5K   209  
Give a C++ application its own web page
// webemMT.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"

#include <boost/asio.hpp>
#include <boost/bind.hpp>
#include <boost/function.hpp>

#include "../../cWebem.h"

/// An application class which says hello
class cHello
{
public:
	char * DisplayHTML()
	{
		return "<em>Hello World</em>";
	}
	/**
	Hello to the wide world, returning a wide character UTF-32 encoded string with chinese characters

	*/
	wchar_t * DisplayWWHello()
	{
		return L"Hello Wide World.  Here are some chinese characters: \x751f\x4ea7\x8bbe\x7f6e";
	}
};


int _tmain(int argc, _TCHAR* argv[])
{
	// Initialize application code

	cHello hello;

	// Initialise web server.

    http::server::cWebem theServer(
		"0.0.0.0",						// address
		"1570",							// port
		".\\");							// document root

	// register application method
	// Whenever server sees <!--#webem hello -->
	// call cHello::DisplayHTML() and include the HTML returned

	theServer.RegisterIncludeCode( "hello", 
		boost::bind( 
		&cHello::DisplayHTML,	// member function
		&hello ) );			// instance of class

	// register wide character application method
 	// Whenever server sees <!--#webem wwwhello -->
	// call cHello::DisplayWWHello() and include the HTML returned, converted to UTF-8
   
	theServer.RegisterIncludeCodeW( "wwwhello", 
		boost::bind( 
		&cHello::DisplayWWHello,	// member function
		&hello ) );			// instance of class


	// Tell user where to find the cWebem GUI
	printf("%s",theServer.Splash().c_str());

	// run the server
	theServer.Run();

	return 0;
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The BSD License


Written By
Founder Raven's Point Consulting
Canada Canada
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions