Chapter 5. TCP Client/Server Example


We will now use the elementary functions from the previous chapter to write a complete TCP client/server example. Our simple example is an echo server that performs the following steps:

  1. The client reads a line of text from its standard input and writes the line to the server.

  2. The server reads the line from its network input and echoes the line back to the client.

  3. The client reads the echoed line and prints it on its standard output.

Figure 5.1 depicts this simple client/server along with the functions used for input and output.

Simple echo client and server.

Figure 5.1. Simple echo client and server.

We show two arrows between the client ...

Get The Sockets Networking API: UNIX® Network Programming Volume 1, Third Edition now with O’Reilly online learning.

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