Introduction to TCP

Introduction to TCP

TCP and UDP are two transport layer protocols used to carry port numbers. We use these protocols to connect one process on a system to another process on the other side. Normally when a device sends a packet to a destination, it is meant to be collected and processed by a process (an application layer protocol like HTTP). These applications are given a number called a port number. For example, HTTP server’s port number is 80. So if we send a packet to port 80 on a device, the HTTP server will accept and respond to it, provided that the device has an HTTP server installed and running. Transport layer protocols like TCP are responsible for carrying these port numbers.

TCP comes with a lot of features. It can provide connection-oriented communication, reliable data transfer, and a few others. UDP, on the other hand, doesn’t offer anything beyond carrying port numbers. But it does have its advantages, it’s faster than TCP and it can also work with broadcast or multicast packets.

Now let’s check the main features that TCP can provide:

  1. TCP provides connection-oriented communication
  2. TCP can provide reliable data transfer
  3. TCP can provide sequencing
  4. TCP can provide error control
  5. TCP provides congestion control and flow control

Even though TCP has many good features, the main disadvantage is that these make TCP slower than UDP. It also cannot support multicast and broadcast communication.

Connection-oriented communication

When an upper layer protocol like HTTP gives some data to TCP, TCP will first check whether the other side is available to receive it. TCP will try to transfer the data only if the other side is ready to accept it. TCP uses some flag bits to do this. It uses the flags SYN (synchronize) and ACK (acknowledgment) to establish the connection. Note that this is a virtual connection. We don’t need a physical cable for this. The connection means we did a handshake with the other side, asked if it is ready to accept the data, and after getting permission, we transfer the data.

A diagram of the TCP three-way handshake showing the sequence of segments with SYN, SYN-ACK, and ACK flag bits.
TCP three way handshake

Reliable data transfer (Sequence and Acknowledgement numbers)

TCP uses sequence numbers and acknowledgement numbers to keep track of the segments transmitted. When TCP receives data from the upper layer, it gives a number to each byte. This is called the byte number. Then the data is divided into smaller units (segments) to send to the destination. Each unit is given a TCP header with a sequence number equal to the byte number of the first byte of that segment.

When a segment is received at the destination, it will send back a response with an acknowledgement number equal to the next sequence number it is expecting.

An illustration of TCP byte numbers and sequence numbers, showing how each byte is numbered by the upper layer and how the first byte number is used as the sequence number in the TCP header.
TCP Byte numbers and Sequence numbers

For example, let’s assume the source sends a segment with byte numbers 1 to 9 (as shown in the figure), the sequence number for this segment will be 1 and if this segment is delivered, the destination will respond with a TCP segment with acknowledgement number 10.

Note that TCP can’t absolutely “guarantee” delivery in cases like persistent network failure. The guarantee is conditional on the connection being maintained.

Error control in TCP

TCP has a 16-bit checksum field in the header to check for errors in the data section, TCP header, and some parts of the IP header. The destination can detect if any bit has changed in these sections. If it finds an error, it won’t send an acknowledgement, which will make the source retransmit the segment.

To calculate the checksum, TCP first finds the sum of all 16-bit words in the data section and pseudo header (TCP header and partial IP header). Then the 2’s complement of this sum is used as the checksum.

Window size

As we saw earlier, when the source sends a segment, the receiver will respond with an acknowledgement. But waiting for acknowledgement for each segment can slow down the whole process. To speed things up, TCP can send multiple segments together, and the receiver can send a single acknowledgement for all of them.

To do this, the source must know how many bytes the receiver can handle at a time. TCP header has a field called window size for this. Each device informs its buffer capacity to the other side using this 16-bit field. The values can range from 0 to 65,535 depending on memory. Window size can be increased or decreased dynamically during communication.

Flow control

When the receiver cannot handle too much data at once, it can tell the sender to slow down. This process is called flow control. TCP manages this by using the window size field in its header.

For example, if the receiver first advertises a window size of 50,000 bytes during the three-way handshake, but later if it cannot handle incoming data fast enough, it can reduce the window size to something smaller, like 100. From that point, the sender can send only 100 bytes at a time and must wait for an acknowledgment before sending more. This way, the receiver is not overloaded and packet loss is avoided.

Congestion control

Congestion control is similar to flow control, but here the problem happens at an intermediate router. That router will signal congestion. Routers use the CE flag in the IP header to tell the destination they are congested. The destination then reduces the window size and informs the source.

When the source receives this new window size, it must accept and respond with the CWR flag. In the segment where the destination informs the reduced window size, it will also set the ECE flag. This shows the problem is not at the destination but at an intermediate device.

Disadvantages of using TCP

TCP has many good features like reliability, connection-oriented communication, and error control. But these same features make it slower compared to UDP. That is why not all applications use TCP.

For example, in video calls, audio calls, or live games, UDP is preferred. In these cases, speed matters more than reliability. There is no use retransmitting data in these applications where real-time communication is important.

Another issue is that TCP cannot support multicast and broadcast traffic. So protocols like DHCP, mDNS, RIP, etc. use UDP instead of TCP.



We’d love to hear your feedback and suggestions about this article. Feel free to reach out to us using the WhatsApp number below.

Sajith Achipra has been a trainer and testing consultant at Zframez Technologies since 2009. With 15+ years of experience, he specializes in networking, Python, development, and testing. He conducts online courses to help students and professionals enhance their skills. You can reach him on WhatsApp at +91 8884 884 844 for your training and testing requirements.