establish a Maple server
positive integer; port number on which the service is to be made available
Maple procedure (or other expression); use to service a single request
To establish your own Maple services, you can use the Serve command which runs a server on a specified port. It requires two arguments: a port number port on which to listen for requests, and a Maple procedure server which handles a single incoming request. The call Serve( port, server ) establishes a service on the specified port (assuming that you have permissions to do so), and listens for requests on that port. When a request arrives, the callback server is called with the socket ID of the incoming request as its only argument. The server procedure should service the request and return without error.
Any exception raised in the server callback causes the service to be interrupted and the exception propagates to the Maple process that invoked the Serve call.
Note: Due to licensing considerations, Serve does not establish a true server; you must manually put the Maple server process into the background, and only one incoming request at a time is handled. No new threads or processes are created to service individual requests, even on the UNIX platform.
The procedure Serve is called for effect, and never returns.
The procedures Sockets[GetPeerHost] and Sockets[GetPeerPort] may be useful in authenticating incoming client requests.
If you use a system tool such as ``netstat'' to examine the sockets that are open, you may notice connections allocated in a ``TIME_WAIT'' state after the server has finished servicing a request. This is a normal effect of the TCP protocol, which keeps sockets allocated in a TIME_WAIT state for some period of time (twice the ``maximum segment lifetime'', or four minutes, by default) after the connection is closed. When a socket is in the TIME_WAIT state, the operating system is ensuring that any network packets that arrive with the allocated socket's address are bound to it and not to a new connection that might otherwise be bound to the same address as the old connection. For further details, see RFC 761 (``Transmission Control Protocol'') Most modern TCP implementations use the so-called ``SYN->RCVD'' transition that allows the operating system to open a new connection directly from the TIME_WAIT state providing that certain conditions are met. See RFC 1122 (``Requirements for Internet Hosts -- Communication Layers''), Section 220.127.116.11 for details.
This example illustrates a simple greeting server.
server := proc( sid )
use Sockets in
Write( sid, sprintf( "Hello %s on port %d, from %s\r\n",
GetPeerHost( sid ), GetPeerPort( sid ), GetHostName() ) )
The following call causes the Maple session in which it is invoked to start servicing requests. The call never returns.
Download Help Document