Simple Server Sample in C++

The simple HTTP server sample demonstrates how to add HTTP server functionality to your C++ application.

The sample is a multithreaded main program that listens on port 8888 for HTTP requests. By changing the value of the ThreadLimit directive in the configuration file to zero you can run single-threaded.

See also the equivalent C simpleServer sample.

Files

simpleServer.conf
simpleServer.cpp

Configuration File

simpleServer.conf

DocumentRoot "."
Listen 8888
ThreadLimit 4

LoadModule ejs ../../../lib/libejsModule
LoadModule egi ../../../lib/libegiHandler
LoadModule esp ../../../lib/libespHandler
LoadModule static ../../../lib/libcopyHandler

AddHandler egiHandler .egi
AddHandler espHandler ESP
AddHandler copyHandler

This configuration file loads the embedded JavaScript module and the embedded server pages and embedded gateway interface, and static content handlers. It is configured to run with 4 pool threads. It assumes that the sample is being run from the samples C/simpleServer directory and so the module paths are relative to the lib directory in the samples source tree. Modify these module paths to suit your installation.

You should modify the DocumentRoot and Listen directives to suit your application's needs.

Source Code

simpleServer.cpp

//
// Copyright (c) Mbedthis Software LLC, 2003-2004. All Rights Reserved.
//
/// @file simpleServer.cpp
/// @brief Embed the AppWeb server in a simple multi-threaded
// application.
///

/////////////////////////////// Includes ///////////////////////////////

#include "appWeb/appWeb.h"


/////////////////////////////////// Code ///////////////////////////////

int main(int argc, char** argv)
{
Http *http;
Server *server;
Mpr mpr("simpleServer");

//
// Do the following two statements only if you want debug trace
//
mpr.addListener(new LogToFile());
mpr.setLogSpec("stdout:4");

//
// Start the Mbedthis Portable Runtime.
//
mpr.start(0);

//
// Create Http and Server objects for this application. We set the
// ServerName to be "default" and the initial serverRoot to be ".".
// This will be overridden in simpleServer.conf.
//
http = new Http();
server = new Server(http, "default", ".");

//
// Configure the server with the configuration directive file
//
if (server->configure("simpleServer.conf", 0) < 0) {
fprintf(stderr,
"Can't configure the server. Error on line %d\n",
server->getLine());
exit(2);
}

//
// Start the server
//
if (http->start() < 0) {
fprintf(stderr, "Can't start the server\n");
exit(2);
}

//
// Tell the MPR to loop servicing incoming requests. We can
// replace this call with a variety of event servicing
// mechanisms offered by AppWeb.
//
mpr.serviceEvents(0, -1);

//
// Orderly shutdown
//
http->stop();
delete server;
delete http;

return 0;
}



© Mbedthis Software LLC, 2003-2204. All rights reserved. Mbedthis is a trademark of Mbedthis Software LLC.