Client/Server

Sockets

A door between an application process and the transport protocol (be it TCP or UDP). Applications may have many sockets.

Client / Server Interaction

Server starts by getting ready to receive client connections. The server must be running and listening before a client can initiate a communication.

Client

  • Create a TCP socket
  • Communicate
  • Close the connection

Server

  • Create a TCP socket
  • Repeatedly
    • Accept new connection
    • Communicate
    • Close connection
  1. Start an endless loop to deal with each conversation with the client. Without a loop, the server would communicate with one client, then terminate
  2. The server waits (is blocked) on the client beginning the communication
  3. Client begins communication with the server. A socket is created to point to the server, using the same port that the server is using
  4. Client sends data via a stream to the server. The stream gets sent to the socket
  5. Data sent by the client is detected by the server and the connection is accepted
  6. Data is read from the socket on the server's side in "buffers"

The server can send data back to the client by the same socket used to receive client data. Some data might be large, so it can be split up into several smaller chunks.