Understanding Sockets in Unix, NT, and Java
By Ken Nordby2003-05-26
Sample sockets application
Let's look at three illustrative programs that constitute a simple client-server application based on sockets technology. This sample application is drawn from the Unix environment, and exploits the common Unix command man, which displays documentation about other Unix commands in the form of man pages. The purpose of the sample application is to make man pages available to users on non-Unix systems. Unix users are familiar with the man command, which retrieves documentation from the online Unix manual. For example, the command man sync displays documentation describing the purpose, syntax, and usage of the sync command.
Obviously the man command is intended to be run on Unix systems, because it displays documentation about Unix commands. What if a programmer working in a multi-platform environment wishes to retrieve man output while working on a Windows NT machine? Although this user requirement is artificial, the solution illustrates the capabilities of a client-server application based on sockets.
Client-Server Model
The sample application makes use of the client-server model of software design, which distributes the computing load across two or more machines. In the sample application, the server process runs on a Unix machine. The server process spends most of its time idle, waiting for a request from a client machine. The server opens a socket and makes it available on the network, providing addressability by hostname (bookworld) or by IP address (9.37.195.88). The server then goes into a quiesced listening mode. In this application the server is not entirely passive, because after every three idle minutes it notifies the user and prompts for approval to continue.
The sample application contains two client programs (one written in C++, one written in Java) that run on Windows NT machines. When a client is launched, it opens a socket and attempts to connect to the server process. On the local machine it prompts the user for a Unix command, which it transmits through the socket to the server. The server process constructs a man command string containing the client data, runs the command, and captures the output. The server transmits the output back to the requesting client, which displays it on the screen to the user. Thus, by way of sockets, the NT user has access to man page information while using a non-Unix machine. In this client-server configuration, all interprocess communication takes place by means of sockets.
Example of Unix man command |
Tutorial Pages:
» Understanding Sockets in Unix, NT, and Java
» Basic sockets concepts
» A simple example
» Sample sockets application
» Server program (Unix)
» Client program (NT)
» Client program (Java)
» Further study
First published by IBM DeveloperWorks
