Comparing TCP and UDP


TCP (Transmission Control Protocol) and UDP (User Datagram Protocol) are the most important protocols operating at the transport layer of the OSI model.
In this section, we will explain the function of TCP and UDP and their key roles in the transport layer of the OSI model.


What is TCP?

Transmission Control Protocol (TCP) is a layer 4 protocol of the OSI model. It is considered to be connection-oriented and designed to provide reliable, ordered, and error-free delivery of segments between the sender and receiver.

Functions of TCP

  • Connection Establishment: TCP establishes a connection between sender and receiver using a three-way handshake before data transmission begins.
  • ​​Reliable Data Delivery: TCP ensures that all data is delivered accurately by using acknowledgments and retransmitting lost segments.
  • ​​Segmentation and Reassembly: TCP divides large data into smaller segments and reassembles them correctly at the destination.
  • ​​Sequencing (Ordered Delivery): TCP assigns sequence numbers to segments so that data is received in the correct order.
  • ​​Error Detection and Correction: TCP detects errors using checksums and ensures corrupted data is retransmitted.
  • Flow Control: TCP controls the rate of data transmission to prevent overwhelming the receiver.
  • Congestion Control: TCP adjusts the data transmission rate based on network conditions to avoid congestion.
  • Full-Duplex Communication: TCP allows simultaneous two-way communication between sender and receiver.
  • Connection Termination: TCP terminates the connection gracefully after data transfer is complete.

What is UDP?


UDP stands for User Datagram Protocol. It’s a core protocol operating at layer 4 of the OSI model and is responsible for sending data over a network quickly and with minimal overhead. It does not guarantee delivery of segments.

Functions of UDP

  • Connectionless Communication: UDP transmits data without establishing a connection between sender and receiver.
  • ​Fast Data Transmission: UDP provides faster communication by eliminating connection setup and control mechanisms.
  • Unreliable Delivery: UDP does not guarantee delivery of data, and packets may be lost without retransmission.
  • ​No Sequencing: UDP does not maintain the order of packets, so data may arrive out of sequence.
  • ​No Error Recovery: UDP performs basic error checking but does not correct errors or retransmit lost data.
  • ​Low Overhead: UDP has minimal protocol overhead due to the absence of complex control features.
  • ​Process-to-Process Communication: UDP uses port numbers to deliver data to the correct application.
  • Supports Broadcasting and Multicasting: UDP allows sending data to multiple recipients simultaneously.
  • Suitable for Real-Time Applications: UDP is ideal for applications like streaming and online gaming where speed is more important than reliability.

Functions of TCP (Transmission Control Protocol)

1. Connection Establishment using Three-way Handshaking in TCP

In TCP (Transmission Control Protocol), a connection between the sender and the receiver needs to be established before actual data transfer occurs. This is done by using a process called the 3-way handshake. This ensures both sender and receiver are ready for reliable communication.


Here is the step-by-step procedure for the three-way handshaking process.

Three Way Handshaking Process in TCP

Step 1: SYN (Synchronize)

  • The client (the device initiating the connection) sends a SYN packet to the server.
  • This packet indicates the client wants to start a connection.
  • It also includes an initial sequence number (ISN) to keep track of data.
  • At this point, the client enters a state called SYN-SENT.

Step 2: SYN-ACK (Synchronize-Acknowledge)

  • The server receives the SYN request from the client.
  • It responds with a SYN-ACK packet. SYN confirms it also wants to establish a connection, while ACK acknowledges the client’s sequence number.
  • The server generates its own sequence number. The server moves into the SYN-RECEIVED state.

Step 3: ACK (Acknowledge)

  • The client receives the SYN-ACK packet.
  • It sends back an ACK packet to the server.
  • This acknowledges the server’s sequence number.
  • The client enters the ESTABLISHED state.
  • Once the server receives this ACK, it also transitions to ESTABLISHED.

2. How Segmentation and Reassembly Occur in TCP

Once a connection is established using three-way handshaking in TCP, then the actual transfer of data begins. But the sizes of the data are large and are of varying size; they cannot be beat once. To make things easier, TCP breaks them into smaller pieces and ensures they are properly rebuilt at the destination. This process is called segmentation and reassembly.

Let’s understand them step by step:

Step 1: Data Segmentation (Breaking Data into Pieces)

  • The sender takes a large block of application data (like a file or webpage).
  • It divides the data into smaller units called segments.
  • Each segment is sized based on the Maximum Segment Size (MSS).
  • Every segment is assigned a sequence number.
  • These sequence numbers help track the correct order of data.

Step 2: Transmission of Segments

  • The sender transmits multiple segments across the network.
  • These segments may travel through different paths depending upon the network topology.
  • These segments arrive out of order, get delayed, or even get lost at their destination due to network conditions.
  • TCP keeps track of all sent segments for reliability.

Step 3: Receiving and Ordering Segments

  • The receiver collects all the incoming segments.
  • It checks the sequence numbers of each segment.
  • Segments may be received out of order; hence, they are arranged in the correct order.
  • Retransmitted or duplicate segments are identified and discarded.
  • It also detects missing data and then requests them again using acknowledgments (ACKs).

Step 4: Acknowledgment and Retransmission

  • The receiver sends ACKs (acknowledgments) for successfully received segments.
  • If a segment is missing, the receiver may request retransmission (via duplicate ACKs)
  • The sender retransmits lost segments.

Step 5: Data Reassembly

  • Once all segments are received correctly, the receiver reorganizes them back into the original data stream.
  • The data is further passed to the upper layer of the OSI model.

3. Windowing in TCP (Flow Control Mechanism)

After connection setup and segmentation, Transmission Control Protocol (TCP) needs a way to control how fast data is sent. This is where windowing (or flow control) comes in. It ensures the sender doesn’t overwhelm the receiver with too much data at once.

Let’s break it down clearly:

Step 1: Window Size Advertisement

  • The receiver specifies how much data it can handle at a time.
  • This value is called the window size.
  • It is included in the TCP header of acknowledgment packets.
  • The window size represents the available buffer space at the receiver.

Step 2: Sender Respects the Window

  • The sender checks the receiver’s advertised window size.
  • It sends only that amount of data before waiting for acknowledgment.
  • This prevents buffer overflow at the receiver side.

Step 3: Sliding Window Mechanism

  • TCP uses a sliding window technique.
  • As the receiver processes data, it sends ACKs.
  • With each ACK, the window “slides” forward.
  • This allows the sender to transmit more data continuously.

Step 4: Handling Full Window (Zero Window)

  • If the receiver’s buffer is full:
  • It advertises a zero window size.
  • The sender temporarily stops sending data.
  • The sender periodically checks if the window opens again.

Step 5: Window Update and Resume

  • Once the receiver has free space:
  • It sends a window update with a larger size.
  • The sender resumes transmission.

4. Error Detection and Correction in Transmission Control Protocol

TCP provides reliable communication by detecting errors and correcting them through retransmission.

Error Detection In TCP

  • The error detection is performed by TCP using a checksum mechanism.
  • The sender calculates a checksum value before transmitting the data segment.
  • The checksum is attached to the TCP segment during transmission.
  • The receiver recalculates the checksum after receiving the data. If the calculated checksum matches the received checksum, the data is accepted as correct.
  • If the checksum values do not match, the receiver detects that the data is corrupted.
  • TCP corrects errors by retransmitting the damaged or lost data segments.
  • The receiver sends an acknowledgment (ACK) when data is received correctly.
  • If the sender does not receive an ACK within a specific time, it retransmits the segment.
  • TCP uses sequence numbers to identify missing or out-of-order segments.
  • The receiver rearranges the segments in the correct order using sequence numbers. These mechanisms make TCP communication reliable and error-free.

Error Correction in TCP

  • TCP corrects errors by retransmitting lost or corrupted data segments.
  • The receiver checks the received data for errors using the checksum method.
  • If the data is received correctly, the receiver sends an acknowledgment (ACK) to the sender.
  • If the data is corrupted, the receiver discards the segment.
  • The sender waits for an acknowledgment after sending each segment.
  • If the acknowledgment is not received within a certain time, the sender retransmits the data segment.
  • TCP uses sequence numbers to identify missing or incorrect segments.
  • The receiver rearranges retransmitted segments in the correct order.
  • This retransmission process ensures reliable and accurate data delivery.

5. Congestion Control in TCP

The Transmission Control Protocol uses congestion control to prevent excessive traffic on the network. Congestion control helps avoid network slowdown and packet loss.

TCP monitors network traffic conditions during data transmission. The sender reduces the transmission rate when congestion is detected. TCP increases the transmission speed gradually when the network becomes stable.

TCP uses a congestion window to control the amount of data sent into the network. Packet loss or delayed acknowledgments indicate possible network congestion. TCP algorithms such as slow start and congestion avoidance help manage network traffic efficiently.

Congestion control improves network performance and reliable communication.


TCP Header Segment

A TCP header segment is the structured part of a TCP packet that carries control information required for reliable data communication.

TCP Header Segment
  • The source port identifies the sending application process on the sender’s device.
  • Destination Port identifies the receiving application process on the receiver’s device.
  • A sequence number is used to ensure correct ordering of data segments.
  • The acknowledgment number indicates the next expected byte from the sender.
  • Data Offset (Header Length) specifies the size of the TCP header.
  • Reserved bits are kept for future use and must be set to zero.
  • Control flags define the state of the connection, such as SYN, ACK, FIN, and RST.
  • Window size controls flow control by indicating how much data the receiver can accept.
  • Checksum is used for error detection in the segment.
  • Urgent Pointer points to urgent data when the URG flag is set.
  • The Options field is optional and used for additional features like maximum segment size (MSS).
  • Padding is added to ensure the header length is a multiple of 32 bits.

UDP Header Segment

The UDP header is the first part of a User Datagram Protocol segment that contains control information needed for data transmission between sender and receiver.

The UDP header is simple and has a fixed size of 8 bytes. It helps identify the sending and receiving applications and checks for transmission errors.

UDP Header Segment

The UDP header contains four fields:

  1. Source port identifies the application process that sends the data.
  2. Destination Port identifies the application process that receives the data.
  3. Length field specifies the total size of the UDP header and data together.
  4. Checksum Field is used to detect errors in the UDP segment during transmission.

Because the UDP header is small and simple, UDP provides faster communication with low overhead.


Conclusion

TCP and UDP are important transport layer protocols used in computer networks for data communication.

TCP provides reliable, connection-oriented, and error-checked communication with features such as flow control, congestion control, and retransmission of lost data. It is mainly used in applications where accuracy and reliability are important, such as web browsing, email, and file transfer.

UDP provides fast, connectionless communication with low overhead and minimal delay. It is mainly used in real-time applications such as video streaming, online gaming, and voice calls where speed is more important than reliability.

Thus, TCP is suitable for reliable communication, while UDP is suitable for fast and real-time communication.