TCP and UDP Protocols in Transport Layer

In this section, we explain the features and working of the  most popular and very important protocols of the transport layer of OSI model : TCP and UDP. The  topic will cover the basic functionalities of TCP and UDP, TCP and UDP segments format and a comparison between TCP and UDP.

TCP and UDP in Transport Layer

TCP and UDP are the major transport layer protocols. Before discussing TCP and UDP in detail, let us understand the main functions of the transport layer.

The transport layer in the OSI model deals with the process-to-process communication between the two stations. The data from the upper layer is received by the transport layer and broken down into smaller chunks of a similar size called segments. The segment is further sent to the network layer after encapsulating essential header information of the transport layer. The other major functions of the transport layer are segmentation, reassembly, sequencing, flow control, port addressing etc.

TCP (Transmission Control Protocol) and UDP (User Datagram Protocol) are the major protocols that work at the transport layer for various network application services. TCP is utilized by the service where reliability is the top priority. UDP is used by services for faster communication like real-time applications, live streaming etc.

Transmission Control Protocol (TCP)

  • TCP stands for Transmission Control Protocol.
  • TCP is said to be a connection-oriented protocol. As it establishes a TCP connection between the two stations before sending any data to the receiver. The connection is then terminated after the exchange of data is completed.
  • The session establishment and the termination using the three-way and four-way handshaking respectively. This will be explained in detail in a later section of this article.
  • TCP provides reliable communication. Hence, it is also called a reliable transport layer protocol. The destination must reply with an acknowledgement message after the segment is received. If an acknowledgement  is not  received by the sender, the same segment is retransmitted to ensure reliable communication.
  • Another important function of TCP is packet sequencing. When upper layer data are broken down into smaller segments, each segment is then assigned a sequence number so that when the segments received by the destination at any order are reassembled into the correct sequence or order.
  • Flow control is yet another function in TCP. The flow control mechanism allows both sender and receiver to exchange data at the same data rate. The sender and the receiver synchronize and compromise with the same transmission speed so that the receiver will not be overwhelmed by the senders data rate.
  • The data transfer between the sender and the receiver is carried out  with full duplex mode.

User Datagram Protocol (UDP)

  • UDP stands for User Datagram Protocol.
  • UDP is said to be a connectionless and unreliable transport layer protocol.
  • It provides process-to-process communication in the transport layer.
  • UDP is a simple protocol with minimum overhead.
  • UDP is connectionless because it does not establish any session between the two bodies before sending packets to the receiver.
  • It is also called unreliable because it does not wait for any acknowledgement of the received packet from the receiver, whether the packet has been received without any error.
  • UDP packets are called user datagram and they have  fixed header size of 8 bytes.

What are port numbers in TCP and UDP?

The port numbers are unique numbers used with IP addresses. It ranges from 0 to 65535. Port numbers are controlled and registered by IANA (Internet Assigned Numbers Authority). TCP and UDP use port numbers to identify the different application services.

IANA has classified the port numbers into three board categories:

Well known Ports

It ranges from 0 to 1023. It is controlled and registered by IANA. Popular application services like HTTP, FTP, SMTP, and telnet are assigned fixed well-known ports.

Registered Ports

It ranges from 1024 to 49151. It is not controlled by IANA. It is only registered by IANA to prevent any duplicacy.

Dynamic Ports

It ranges from 49152 to 65535. It is neither controlled nor registered by IANA. Dynamic ports are mostly used by the processes running on the client end.

How does TCP work

TCP is a connection-oriented and reliable protocol. Before sending data to the destination, the source always establishes a TCP connection and then begins exchanging information. The connection is then terminated after the data transmission is over. The establishment and the termination of the TCP connection is performed using a three-way and four-way handshaking mechanism. Later on, we will discuss in detail how three-way and four-way handshaking is done.

Now let us understand the TCP segment formats.

TCP Segment Format

compare tcp and udp protocols- tcp header

Source Port Address

This field contains the port numbers of the sender. When the sender is the client machine, it uses dynamic port numbers that range from 49152 to 65535. When the sender is the service, it uses well-known ports that range from 0 to 1023.

Destination Port address

This field contains the port number of the receiver. When the destination host is the server, then it will be well-known ports. When the destination host is the client, it will be the dynamic ports.

Sequence Number

The TCP sequence number field is a 32-bit value. The value ranges from 0 to 232 – 1 (4,294,967,295). The sequence number tells the receiving host which byte in this sequence comprises the first byte in the segment.

Acknowledgement Number

It is also a 16-bit field. When the ACK flag is set ON, it contains the next sequence number of the data byte and acts as the acknowledgement for the previous TCP sequence received.  For example, If the destination host receives the sequence “X“, then it replies with “X+1” as an acknowledgement number.

HLEN

It is a 4-bit header length field and indicates the length of the TCP header by a 4-byte word in the header. The size of the TCP header ranges from 20 to 60 bytes. For 20 bytes of header, the value of  HLEN field would be 5, because 4 x 5 = 20. For 60 bytes, it would be 15, because 4 x15 = 60. Thus, the value of HLEN field lies between 5 to 15.

Flags

TCP uses six control flags of 1 bit each to perform connection establishment, termination, connection abortion,  flow control, and mode of data transfer.

  • URG – This flag represents the urgent pointer. When it is set ON when the segment needs urgent processing of data.
  • ACK – When ACK is set ON,  the destination host replies with the acknowledgement number for every sequence it receives.
  • PSH– When it is set ON, then it requests the destination host to pus the data without buffering it.
  • RST – This flag is used to restart the connection.
  • SYN–  It synchronizes the sequence numbers during the three-way handshaking.
  • FIN – This flag is used to terminate the connection.

Window Size

It is a 16-bit field and defines the size of the window that the destination host can accept.

Checksum

It is a 16-bit field used for error control. This field is optional in UDP.

Urgent Pointer

The urgent pointer is a 16-bit field to process data urgently. When the URG flag is set ON, it requests for the urgent process.

Options and Padding

This field can be up to the size of 40 bytes.

UDP Header Formatudp header

Source Port Address

It is a 16-bit field that indicates source port numbers. If the client is the source, the source port address will be dynamic ports. On the other hand, if the server is the source, then it will be the well-known ports.

Destination Port Address

This field contains a 16-bit destination port address. If the client is the receiver, the destination port number would be the dynamic port and the server utilizes well-known ports for the destination.

Total Length

The field is a 16-bit length that indicates the length of the UDP segment including the header and the data.

UDP Checksum

The checksum is a 16-bit or 2-byte field. The checksum field is optional in the UDP segment. It depends on the application on which UDP is running. The basic function of the checksum field is to ensure whether the segments are received accurately or not.

Establishing TCP Connection using Three-way Handshaking

three way handshaking

The TCP three-way handshaking process is a three-step process in which the client establishes the virtual connection with the server prior to the exchange of data between them.

Step1: The client sends the TCP segment to the server with the SYN flag set ON.

Step2: In response to the client request, the server sent the TCP segment with SYN flag and ACK flag set to ON.

Step3: Finally, the client replies by sending a TCP segment with the ACK flag set to ON.

This completes the process of three-way handshaking and the TCP connection is established between the client and server, Now, both of them are ready for the exchange of real data between them.

Terminating  TCP Connection using Four-way Handshaking

four way handshaking

After the exchange of data is completed between the client and the server, The TCP connection needs to be terminated.  A four-way handshaking process is performed to terminate the connection.

Step1: The client sends TCP segments with the FIN flag set ON.

Step2: The server replies with the ACK flag set to ON.

Step3: The server then sends TCP segment with FIN flag set.

Step4: Finally the client replies with the ACK flag set to ON.

This will terminate the connection between the client and the server.

Compare TCP and UDP

TCP UDP
  • TCP stands for Transmission Control protocol
  • UDP stands for User Datagram Protocol
  • TCP is a connection-oriented protocol that establishes connections between the hosts before sending any data.
  • UDP is a connection-less protocol. It does not establish a connection between the hosts.
  • TCP is considered to be highly reliable because it always asks for the acknowledgement from the receiver before sending any segment further.
  • It is un reliable because it does not expect or wait for the ACK form the receiver.
  • Header size is 20 bytes.
  • Header size is 8 bytes.
  • Retransmission facility
  • No such facility.
  • TCP is much slower and low efficient as compared to UDP.
  • UDP is faster and more efficient than TCP.

←Prev Next→
Optical Fiber Cable & its Types IP v4 Addressing & Subnetting

Leave a Reply