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:

1.
Create a ServerSocket object.
2.
Create a Socket object from the ServerSocket.
3.
Create an input stream to read input from the client.
4.
Create an output stream that can be used to send information back to the client.
5.
Do I/O with input and output streams.
6.
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 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.