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.c

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.c

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

/******************************* Includes *****************************/

#include "appWeb/appWeb.h"

/********************************* Code *******************************/
#if MPR_FEATURE_C_API

int main(int argc, char** argv)
{
MaHttp *http; /* For the http service inside our app */
MaServer *server; /* For a HTTP server */

/*
* Initialize the run-time and give our app a name "simpleServer"
*/
mprCreateMpr("simpleServer");

/*
* Do the following two statements only if you want debug trace
*/
mprAddLogFileListener();
mprSetLogSpec("stdout:4");

/*
* Start run-time services
*/
mprStartMpr(0);

/*
* Create the HTTP and server objects. Give the server a name
* "default" and define "." as the default serverRoot, ie. the
* directory with the server configuration files.
*/
http = maCreateHttp();
server = maCreateServer(http, "default", ".");

/*
* Configure the server based on the directives in
* simpleServer.conf.
*/
if (maConfigureServer(server, "simpleServer.conf", 0) < 0) {
fprintf(stderr,
"Can't configure the server. Error on line %d\n",
maGetConfigErrorLine(server));
exit(2);
}

/*
* Start serving pages. After this we are live.
*/
if (maStartServers(http) < 0) {
fprintf(stderr, "Can't start the server\n");
exit(2);
}

/*
* Service events. This call will block until the server is exited
* Call mprTerminate() at any time to instruct the server to exit.
* The -1 is a timeout on the block. Useful if you use
* MPR_LOOP_ONCE and have a polling event loop.
*/
mprServiceEvents(MPR_LOOP_FOREVER, -1);

/*
* Stop all HTTP services
*/
maStopServers(http);

/*
* Delete the server and http objects
*/
maDeleteServer(server);
maDeleteHttp(http);


return 0;
}

/**********************************************************************/
#else /* MPR_FEATURE_C_API */

int main()
{
fprintf(stderr, "MPR_FEATURE_C_API is not defined in config.h\n");
exit(2);
}
#endif /* MPR_FEATURE_C_API */



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