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
Makefile
simpleServer.cpp
Configuration File
simpleServer.conf
DocumentRoot "." Listen 8888 ThreadLimit 4 AddHandler copyHandler
This file is an AppWeb configuration file.
It is configured to run single-threaded and assumes that
the sample is being run from the current directory.
You should modify the DocumentRoot and Listen directives
to suit your application's needs.
Makefile
The Makefile will build on Windows or Linux. A Windows VS 6.0 project
file is also supplied.
Typical output from the Makefile build is listed below. This is the
output on a Windows system:
cl -o simpleServer.exe simpleServer.cpp -Zi -Od -D_NDEBUG -W3 -nologo -MDd -FD -DWIN -D_DLL -D_MT \
-D_WINDOWS -DWIN32 -D_WIN32_WINNT=0x500 -D_X86_=1 -D_CRT_SECURE_NO_DEPRECATE -D_USRDLL \
-I../../../include ../../../bin/libappWebStatic.lib ws2_32.lib advapi32.lib user32.lib
Source
Code
simpleServer.cpp
///
/// @file simpleServer.cpp
/// @brief Embed the AppWeb server in a simple multi-threaded
// application.
//
// Copyright (c) Mbedthis Software LLC, 2003-2005. All Rights Reserved.
//
/////////////////////////////// Includes ///////////////////////////////
#include "appWeb/appWeb.h"
/////////////////////////////////// Code ///////////////////////////////
int main(int argc, char** argv)
{
MaHttp *http; // Http service inside our app
MaServer *server; // For the HTTP server
Mpr mpr("simpleServer"); // Initialize the run time
#if BLD_FEATURE_LOG
//
// Do the following two statements only if you want debug trace
//
mpr.addListener(new MprLogToFile());
mpr.setLogSpec("stdout:4");
#endif
//
// 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 MaHttp();
server = new MaServer(http, "default", ".");
//
// Activate the copy module and handler
//
new MaCopyModule(0);
//
// Configure the server with the configuration directive file
//
if (server->configure("simpleServer.conf") < 0) {
mprFprintf(MPR_STDERR,
"Can't configure the server. Error on line %d\n",
server->getLine());
exit(2);
}
//
// Start the server
//
if (http->start() < 0) {
mprFprintf(MPR_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;
//
// MPR run-time will automatically stop and be cleaned up
//
return 0;
}
|