How a TCP server works, for HTTP developers

A HTTP server uses TCP for communication. At the core of a HTTP server is a TCP server. Understanding how the later works is essential for mastering the development of efficient and resilient web applications.

So, how does it work? Well, let’s get right to it.

The TCP server

A TCP server application starts by opening a passive socket. This passive socket will be used to accept incoming client connection requests. Let’s call this passive socket the acceptor.

For the operating system to start accepting requests for the server endpoint, the acceptor socket must be set to listening mode. The endpoint is a pair of values consisting of an IP address and a protocol port number that uniquely identifies a particular application running on a particular host in a computer network.

With the acceptor in listening mode, the operating system allocates a queue for the incoming pending connection requests and starts accepting them.

When a connection request is in the queue, it is available for processing by the server application. When the server application is available to handle a new request it will get it from the queue and process it.

The acceptor socket is only used to establish connections with the client applications. It is not used for any further communication with the client.

For the server to be able to communicate with the client an active socket must be created and bind with the an endpoint, chosen by the operating system, that points to the client application that requested the connection.

At this point the active socket is ready for being used to server-client communication and the acceptor is available to process the next queued pending connection request.

Know you have an overview of how a TCP server works.

Further reading

Some resources you should investigate for going deeper on this subject:

0%