Home What's New Downloads Programs CSS Editor TCP/IP rInstaller R R2 Java Publications ODBC Browser Internet Server Red Corona Mail me |
A General-Purpose Internet Server using Callbacksby Richard SmithNote: this page uses the fonts APL2741 or APLSans, both available from Causeway. AbstractDuring recent months, I have been involved in the writing of many Internet and Web servers, culminating of course in the multi-user client/server system written for Adapta DLS. It struck me that I was writing almost the same code every time I wrote a new server (socket event handlers such as Receive were the worst), so, following the principles of XP the answer was to put this functionality into a utility library (i.e. a namespace) and to call it from all of the servers. The CriteriaThere are two criteria which are extremely important in the design of such a namespace:
The DesignResponding to a RequestThe server and client namespaces work using a callback system. By using APL style sockets, I could choose any message format I wanted, although you will see that the system I use could easily be converted to text sockets. A message consists of three parts:
To respond to a message you need to register a callback. You can do this using the function Register, which takes a code on the left and an executable string on the right; for example:
At this point it is best to take a step back and try to see what is going on here. Firstly, both these functions will reply to any message that starts with a code of 1 with another message of code 1 (yes, this means that if you set up your client and server like this they'll keep exchanging messages for ever). Also, it is a good idea to see the reasons behind the differences in these two functions:
Identifying ClientsThis is all very well for replying to a message. However if you want to broadcast a message, or send a copy to the user logged in as administator, etc. then you need to know who's connected to your server. Whenever a new connection is received (specifically, in the Accept event of the socket), the ‘clients table is updated. This is a matrix of which the first two columns contain the socket number and IP address of a client; the rest of the table is application-specific data such as login name. To send a message to a particular client, use something of the form:
A broadcast is similar; use Error HandlingTo handle errors, you need to register callbacks on the special codes This article is based on a presentation given at the VikAPL 2000 conference in Gilleleje, Denmark. Page and site © Richard Smith 2001 Please send any comments to richard@redcorona.com - thank you. |
|
|