A typical client-server program flow chart

Stream-oriented socket programs generally follow a prescribed sequence. See Figure 1 for a diagram of the logic flow for a typical client and server. As you study this diagram, keep in mind the fact that a concurrent server typically starts before the client does, and waits for the client to request connection at step 3. It then continues to wait for additional client requests after the client connection is closed.

A typical client-server session

  • Step 1: Server and client create a stream socket s with the socket() call.
  • Step 2: (Optional for client) Sever bind socket s to a local address with the bind() call.
  • Step 3: Server uses the listen() call to alert the TCP/IP machine of the willingness to accept connections.
  • Step 4: Client connects socket s to a foreign host with the connect() call.
  • Step 5: Server accepts the connection and receives a second socket, for example ns, with the accept() call.
  • Step 6 and 7: Server reads and writes data on socket ns, client reads and writes data on socket s, by using send() and recv() calls, until all data has been exchanged.
  • Step 8: Sever closes socket ns with the close() call. Client closes socket s and end the TCP/IP session with the close() call. Go to step 5.
Figure 1. A typical client-server session
Diagram that shows the logic flow of a typical client-server session.