Layer 4 – the Transport Layer

It provides a transparent transfer of data between hosts. It can provide end-to-end recovery and flow control, where flow control stands for process of adjusting the flow of data from the sender to ensure that the receiver can handle all of it).

It supports session multiplexing, the process by which a host is able to support multiple sessions simultaneously and manage the individual traffic stream over a single link.

In picture 4.1 an example of session multiplexing is presented. It is possible to send an HTTP request to a Receiver 1, an SMTP request to a Receiver 2 and an SMTP request to the same Receiver 1 simultaneously using just a single link.

Port Numbers

Layer 4 uses destination port numbers to identify which upper layer protocol is used.

The sender adds a source port number to the layer 4 header. When the receiver sends traffic back, it flips the source and the destination port number (see picture 4.1.1). The combination of the two ports can be used to track sessions, for example by firewalls.

TCP - Transport Control Protocol

The TCP is a Layer 4 protocol with the following characteristics:

  • Connection Oriented: once a connection is established, data can be sent bidirectionally over that connection

  • Reliable: it creates a reliable connection through a process called three way handshake and it works as showed in picture 4.2.1: the sender sends the packet (SYN, synchronize message) to a receiver. The receiver sends back a SYN-ACK (where ACK stands for acknowledgment) packet back to the sender, which answers with a ACK packet to the receiver. This ensures that the packets aren’t lost, and if they are they are immidiatly resent.

  • It carries out the sequencing to ensure that the segments are processed in the correct order and none are missing

  • It performs a flow control

UDP - User Datagram Protocol

The UDP is a Layer 4 protocol that works in the opposite way of TCP, thanks to the following characteristics:

  • It is not connection oriented
  • There is no handshake connection between hosts
  • It does not carry out sequencing, therefore it’s impossible to know if the packets all arrived and arrived in the right order
  • It is not reliable - there is not an ACK message of any sort
  • There is not a flow control
  • Any error detection has to be made by the upper layers

TCP vs UDP

TCP is used by application developers any time the traffic needs to be reliable.

There are real-time applications (like voice or video streams) that can’t afford the extra overhead of the TCP header, so UDP has to be used.

However, there are still some applications that can both use TCP and UDP.

Common TCP and UDP Protocol Ports

LAYER 4 PROTOCOL PORT NUMBER UPPER LAYER PROTOCOL
TCP 21 FTP
22 SSH
23 Telnet
80 HTTP
443 HTTPS
UDP 69 TFTP
161 SNMP
TCP and UDP 53 DNS (usually UDP is preferred)