Both the client and the server have a very common algorithm, which we have to describe in order for you to understand and generalize this concept. The client's algorithm is as follows:
socket() -> connect() -> send() -> receive()
Here, connect() and receive() are blocking calls (that is, the calling program will wait for their completion). The connect phrase specifically initiates the three-way handshake that we described in detail in the Learning the basics of connection-oriented communication recipe.
The server's algorithm is as follows:
socket() -> bind() -> listen() -> accept() -> receive() -> send()
Here, accept and receive are blocking the call. Let's now analyze in detail both the client's and server's code.
The client ...