The main difference between TCP and UDP is that TCP has an additional task: to provide reliable message delivery while using the unreliable IP protocol as its foundation.

To solve this task, TCP uses data transfer with a logical connection established in advance. A logical connection allows the participants in the exchange to make sure that data is not lost, corrupted, or duplicated, and that it arrives at the recipient in the same order in which it was sent.

TCP establishes logical connections between application processes, and each connection involves only two processes. A TCP connection is duplex, meaning each participant in the connection can receive and send data at the same time. The figure shows networks connected by routers running the IP protocol. TCP protocol modules installed on the end nodes solve the task of reliable data exchange by establishing logical connections with each other.

Logical connection diagram

When a logical connection is established, TCP modules agree with each other on the parameters of the data exchange procedure. In TCP, each side of the connection sends the following parameters to the opposite side:

  • the maximum segment size it is ready to receive;
  • the maximum amount of data (possibly several segments) that it allows the other side to send in its direction, even if that side has not yet received an acknowledgment for the previous portion of data (window size);
  • the initial sequence number of the byte from which it starts counting the data stream within this connection.

As a result of negotiation between TCP modules on both sides of the connection, the connection parameters are defined. Some of them remain constant throughout the communication session, while others change adaptively.

The connection is established at the initiative of the client part of the application. When the application client needs to exchange data with the server part, it contacts the lower-level TCP protocol. In response, TCP sends a segment requesting connection establishment to the TCP protocol running on the server side (figure a). Among other things, the request contains the SYN flag set to 1.

Connection establishment and termination

After receiving the request, the TCP module on the server side tries to create the “infrastructure” for serving the new client. It asks the OS to allocate certain system resources for buffers, timers, and counters. These resources are assigned to the connection from the moment it is created until it is terminated. If all necessary resources are obtained and all required actions are completed on the server side, the TCP module sends the client a segment with ACK and SYN flags. In response, the client sends a segment with the ACK flag and moves to the established logical connection state (ESTABLISHED). After receiving the ACK flag, the server also moves to the ESTABLISHED state. At this point, the connection establishment procedure ends, and the sides can proceed to data exchange. The connection can be terminated at any time at the initiative of either side. To do this, the client and server must exchange FIN and ACK segments in the sequence shown in figure b (here the initiator is the client). The connection is considered closed after some time, during which the initiating side makes sure that its final ACK signal arrived normally and did not cause any “emergency” messages from the server.

A logical TCP connection is uniquely identified by a pair of sockets defined for that connection by the two interacting processes.