Fundamentals 20 min read

Mastering the Transport Layer: TCP, UDP, Sockets, and Port Mechanics

This article explains the role of the transport layer in the OSI model, compares TCP and UDP protocols, describes socket types and their APIs, clarifies port number usage, and details multiplexing, demultiplexing, and UDP packet structure with checksum calculation.

Liangxu Linux
Liangxu Linux
Liangxu Linux
Mastering the Transport Layer: TCP, UDP, Sockets, and Port Mechanics

Transport Layer Overview

The transport layer (OSI layer 4) sits between the application and network layers and provides end‑to‑end communication between processes on different hosts. It adds a transport header to the data, enabling logical communication without involving routers, which only forward packets based on network addresses.

TCP and UDP Basics

TCP (Transmission Control Protocol) offers a reliable, connection‑oriented service. It guarantees ordered delivery, retransmits lost packets, and provides flow control.

UDP (User Datagram Protocol) provides an unreliable, connection‑less service; data are sent without a handshake, which makes UDP suitable for latency‑sensitive applications.

How does TCP know which port a packet belongs to?

Socket Fundamentals

A socket is an API endpoint that connects the application layer with the network layer. It exposes functions for setting IP addresses, port numbers, and for sending/receiving data.

Socket Types

Datagram sockets

: use UDP, provide no connection guarantee, and may lose or duplicate data. Stream sockets: use TCP, provide reliable, ordered delivery. Raw sockets: allow direct sending/receiving of IP packets without transport‑layer formatting.

Socket Lifecycle

Create a socket, obtaining a socket descriptor.

Bind the descriptor to a local address and port (servers must bind).

Server calls listen to indicate readiness for connections.

Client calls connect to request a connection.

Server accepts with accept after successful bind and listen.

Both sides use read / write (or send / recv) to exchange data.

When finished, close releases resources.

Port Numbers

Ports are 16‑bit unsigned integers (0‑65535) used to differentiate multiple applications on the same host. They are divided into:

Well‑known ports (0‑1023)

Registered ports (1024‑49151)

Dynamic/private ports (49152‑65535)

Communication is identified by the tuple source IP, destination IP, source port, destination port. For UDP the relevant tuple is a two‑tuple (IP + port); for TCP it is a four‑tuple, enabling multiplexing and demultiplexing of concurrent flows.

UDP Deep Dive

UDP transmits datagrams without establishing a connection, offering low latency and an 8‑byte header (vs. TCP’s 20‑byte header).

Fast transmission – no handshake.

No connection state – no buffers or sequence numbers.

Small header reduces per‑packet overhead.

Reliability can be added at the application layer.

UDP Packet Structure

A UDP datagram consists of a header and a data section. The header contains four 16‑bit fields: Source Port: identifies the sending application (optional, 0 if omitted). Destination Port: identifies the receiving application. Length: total length of header plus data (minimum 8 bytes). Checksum: 16‑bit one's‑complement sum for error detection.

The checksum is calculated by summing all 16‑bit words of the header and data, discarding overflow bits, and then taking the one's complement. The receiver verifies that the final sum equals all 1s; otherwise an error is detected.

Why does UDP provide error detection if it offers no retransmission?

UDP follows an end‑to‑end design principle: it reduces the probability of transmission errors to an acceptable level, leaving reliability mechanisms (e.g., acknowledgments, retransmission) to the application if needed.

Overall, the transport layer enables reliable (TCP) or fast (UDP) communication, with sockets and ports serving as the interface between applications and the network.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

TCPNetwork ProtocolsUDPMultiplexingSocketstransport layerPort Numbers
Liangxu Linux
Written by

Liangxu Linux

Liangxu, a self‑taught IT professional now working as a Linux development engineer at a Fortune 500 multinational, shares extensive Linux knowledge—fundamentals, applications, tools, plus Git, databases, Raspberry Pi, etc. (Reply “Linux” to receive essential resources.)

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.