Some Useful Servers
This section shows several servers you can build with server sockets. It starts with a server you can use to test client responses and requests, much as you use Telnet to test server behavior. Then we present three different HTTP servers, each with a different special purpose and each slightly more complex than the previous one.
Client Tester
In
the previous chapter, you learned how to use Telnet to experiment
with servers. There’s no equivalent program to test clients, so
let’s create one. Example 11.5 is a program
called ClientTester
that runs on a port specified
on thecommand-line, shows all data sent by the client, and allows you
to send a response to the client by typing it on the command line.
For example, you can use this program to see the commands that
Netscape Navigator sends to a server.
Note
Clients are rarely as forgiving about unexpected server responses as servers are about unexpected client responses. If at all possible, try to run the clients that connect to this program on a Unix system or some other platform that is moderately crash-proof. Don’t run them on a Mac or Windows 98, which are less stable.
This program uses two threads: one to handle input from the client and the other to send output from the server. Using two threads allows the program to handle input and output simultaneously: it can be sending a response to the client while receiving a request—or, more to the point, it can send data to the client while waiting for the client to respond. ...
Get Java Network Programming, Second Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.