The Middle Layers: Transport and Network

Layer 4: The Transport Layer

Learn how the Transport layer segments data, uses ports, and chooses between TCP and UDP for reliable or fast delivery.

In this lesson, you will learn to:

  • By the end of this lesson, learners will be able to describe the Transport layer's role, explain ports and sockets, compare TCP and UDP, and choose the appropriate protocol for common scenarios.

Layer 4: The Transport Layer

This lesson explains the Transport layer, the OSI layer responsible for end-to-end communication between applications. Learners will explore segmentation, ports, TCP reliability features, UDP speed, and how to choose between them.

End-to-End Delivery Between Applications

The Transport layer is the fourth layer of the OSI model. Its main job is end-to-end delivery between applications. While the lower layers move data from device to device across a network, the Transport layer makes sure the data reaches the correct application and manages how reliably it travels.

Think of a shipping company that handles packages between two office buildings. The Network layer might decide which roads to take. The Transport layer is like the department that puts the correct suite number on each package, divides a large shipment into smaller boxes, and verifies that every box arrived.

Segmentation and reassembly

Data from the upper layers can be large. The Transport layer breaks large data into smaller pieces called segments. Each segment contains a portion of the data and a Transport layer header. On the receiving side, the Transport layer reassembles the segments back into the original data.

Why segmentation matters

If a large message were sent as one huge block, a single error would require the entire message to be resent. Breaking the data into smaller segments allows only the damaged segment to be retransmitted. It also allows many applications to share the network fairly.

The PDU at Layer 4

The Protocol Data Unit at the Transport layer is commonly called a segment when using TCP. When using UDP, it is often called a datagram. Both names are correct for the Transport layer, but they are tied to specific protocols. Using the right term helps identify whether the sender is using connection-oriented or connectionless transport.

Ports and Sockets

When data arrives at a device, how does the device know which application should receive it? A computer may have a web browser, an email client, a chat program, and many other network applications running at the same time. The Transport layer solves this with ports.

A port is a numerical label from 0 to 65535. Each network service is associated with a port. For example, HTTPS usually uses port 443, DNS uses port 53, and SSH uses port 22. The Transport layer header contains a source port and a destination port so the receiving device can direct the data to the correct application.

What is a socket?

A socket is the combination of an IP address and a port number. It identifies a specific process on a specific device. The source socket and destination socket together identify a complete conversation.

For example, 192.168.1.10:49152 identifies an application on the sending device at that IP address using port 49152. 203.0.113.5:443 identifies a web server using HTTPS at that IP address. Together they describe the whole connection.

Well-known, registered, and ephemeral ports

Ports fall into three ranges:

  • Well-known ports: 0 through 1023, reserved for common services.
  • Registered ports: 1024 through 49151, assigned to applications by request.
  • Ephemeral ports: 49152 through 65535, used temporarily by clients.

When your browser connects to a web server, the destination port is 443. The source port is an ephemeral port chosen by your operating system. The server replies to that ephemeral port, so your computer knows which browser tab should receive the response.

TCP Versus UDP

The two dominant Transport layer protocols are TCP and UDP. They solve the same general problem in very different ways, and choosing between them depends on what the application needs most: reliability or speed.

TCP: Transmission Control Protocol

TCP is connection-oriented and reliable. Before sending data, TCP establishes a connection using a three-way handshake. During the data exchange, TCP:

  • Numbers every segment so the receiver can reorder them.
  • Acknowledges received segments.
  • Retransmits lost segments.
  • Uses flow control to avoid overwhelming the receiver.

TCP is like a certified letter service. Every item is tracked, and the sender knows it arrived. The cost is extra overhead and slight delay. TCP is used by HTTP, HTTPS, FTP, SMTP, and SSH, where all data must arrive intact.

UDP: User Datagram Protocol

UDP is connectionless and best-effort. It does not establish a connection before sending. It does not number segments, acknowledge receipt, or retransmit lost data. UDP simply packages the data and sends it.

UDP is like sending a postcard. It is fast and simple, but there is no guarantee of arrival and no tracking. UDP is used for DNS queries, streaming video, online gaming, and voice over IP, where a small amount of loss is better than delay.

Choosing between TCP and UDP

The choice comes down to a trade-off:

  • Choose TCP when every byte matters. Examples: web pages, email, file downloads.
  • Choose UDP when speed matters and loss can be tolerated. Examples: live video, voice calls, multiplayer games.

Some applications use both. A video call may use TCP for signaling and call setup, then UDP for the actual audio and video stream. This is a powerful example of how different applications choose the right transport protocol for each job.