SuperScript::Software::ucspi-tcphandle

Compiling a tcphandle.c server


The tcphandle.c program provides a framework for compiling preforking tcpserver-like servers.

Each tcphandle server accepts all tcpserver options, and in addition:

Utilize this option if your accept system call suffers from the thundering herd problem.

The concurrency option (-c) sets the number of children that a tcphandle server forks upon startup. The default setting is 40. Each child process listens for requests on the socket inherited from the parent process.

If any of its child processes exits, a tcphandle server forks a new child process to replace the original. When it receives SIGTERM, a tcphandle server sends SIGTERM to each of its children, waits for them to exit, and then itself exits.

Before handling an accepted request, a tcphandle server sets certain environment variables, a la tcpserver.

Server code

A tcphandle server invokes a server subroutine for each request. The subroutine reads from the network on file descriptor 0 and writes to the network on file descriptor 1:
  void server(int argc,char * const *argv)
The arguments to server are the command line arguments to the tcphandle server that remain after option parsing.

The server subroutine is called within a loop, with one iteration per request. It must release any resources allocated to handle a particular request before returning to its caller. This includes changes to the environment.

Compiling a new tcphandle server

Create a file src/myserver.c that implements the server subroutine. Create a control file src/myserver=x listing all necessary dependencies. Then
  build myserver

The ucspi-tcphandle package includes two example servers: tcpprint, and tcpperl.