17.7. Implementing a Server

The server is the program that starts first and waits for incoming connections. Implementing a server consists of six basic steps:

Create a ServerSocket object.
Create a Socket object from the ServerSocket.
Create an input stream to read input from the client.
Create an output stream that can be used to send information back to the client.
Do I/O with input and output streams.
Close the Socket when done.

Each of these steps is described in more detail in the following sections. As with the client, note that most of the methods described throw an IOException, so they need to be wrapped inside a try/catch block in an actual implementation.

Create a ServerSocket object. With a client socket, you ...

Get Core Web Programming, Second Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.