The ServerSocket class is used by a server (or any other program) that wants to sit and wait for a connection request from a client. When you create a ServerSocket object, you specify the port to listen to. To wait for a client rendezvous, call the accept() method. It will block until a client attempts to connect to the port that the ServerSocket object is listening on. When such a request is received, a Socket object is created and returned.

"Note that ServerSocket is not used for communication between the server and the client; it is only used to wait for and establish the connection to the client. Typically, a single ServerSocket object is used over and over again to establish connections to any number of clients." [Flanagan97b, p192]   This is demonstrated in the next example.

The choice of port number 8189 was arbitrary. A number of that size is guaranteed not to clash with any standard servers.

The previous example demonstrated how to set-up an input stream (or read) abstraction. This example adds to that an output stream (or write) abstraction.

This simple server:

The user runs "telnet" as the client application. "" is a special address (called the "local loopback address") that denotes the local machine.

It is not clear how many "streams" need to be closed. Horstmann closes only the socket object [p141], whereas Flanagan closes: the input object, the output object, and the socket object [p193].