Skip to content
Tutorials Center
My learning

Topics

Lesson 35 of 79 ยท Transport Layer

The Transport Layer and Ports

Learn what the transport layer does: process-to-process delivery, ports, sockets, multiplexing and demultiplexing, well-known ports, TCP versus UDP, with live Node code.

The network layer gets a packet from one host to another host. That is not yet enough. A modern computer runs a browser with ten tabs, a chat client, a mail program and a database, all talking at the same time over one network connection. When a packet arrives, the operating system must know which program should receive it. Solving that problem is the first job of the transport layer, the fourth layer of the OSI model.

The transport layer sits between applications and the IP network. It provides end-to-end communication: the two ends are programs on two hosts, and the routers in between never look at transport headers (apart from firewalls and NAT boxes, which peek). The two famous protocols at this layer are TCP, which adds reliability, and UDP, which adds almost nothing. This lesson explains the concepts they share.

What the transport layer does

  • Process-to-process delivery. IP addresses identify hosts. The transport layer adds port numbers to identify programs on a host.
  • Multiplexing and demultiplexing. Many applications share one network interface. The sender's transport layer multiplexes their data into packets, and the receiver's transport layer demultiplexes arriving packets back to the right application.
  • Optional extras. Depending on the protocol: reliable delivery, ordering, flow control, congestion control and error detection. TCP provides all of them; UDP provides only error detection (and even that is optional in IPv4).
flowchart TB
    subgraph H1["Host 1"]
      B1["Browser"]
      M1["Mail client"]
    end
    subgraph H2["Host 2"]
      W["Web server<br/>port 443"]
      S["Mail server<br/>port 587"]
    end
    B1 -- "IP address + port" --> W
    M1 -- "IP address + port" --> S

IP finds the host. The port number finds the program on that host.

A useful comparison is an apartment building: the street address is the IP address and the apartment number is the port.

Ports

A port is a 16-bit number, so there are 65,536 of them (0 to 65535). Every TCP segment and UDP datagram carries a source port and a destination port in its header. IANA divides the range into three groups:

Range Name Use
0-1023 Well-known (system) ports Standard services; on Unix-like systems only privileged programs can listen here
1024-49151 Registered ports Applications registered with IANA, such as databases
49152-65535 Dynamic (ephemeral) ports Temporary ports for the client side of connections

Operating systems differ a bit: Linux picks ephemeral ports from 32768-60999 by default, and Windows follows the IANA range.

Some well-known ports you will meet often:

Port Protocol Port Protocol
20, 21 FTP data, control 110 POP3
22 SSH 123 NTP (UDP)
23 Telnet 143 IMAP
25 SMTP (server to server) 161 SNMP (UDP)
53 DNS (UDP and TCP) 443 HTTPS
67, 68 DHCP server, client (UDP) 587 SMTP submission
80 HTTP 993, 995 IMAPS, POP3S

Others, such as 3306 (MySQL) and 5432 (PostgreSQL), are conventions rather than rules. A server may listen on any port, but clients need to know it, so well-known numbers are the shared agreement. A URL such as https://example.com leaves the port out because https implies 443; http://localhost:8080 names a different port explicitly.

Sockets and the connection identity

Programs reach the network through sockets. A socket is the operating system's endpoint for communication, and you create one with a system call (the socket programming lesson shows the API). Its address is the pair IP address + port, for example 203.0.113.5:443.

A TCP connection is identified by four values: source IP, source port, destination IP and destination port (the 4-tuple, or 5-tuple if you add the protocol). That has an important consequence. A web server listens on one port, 443, and still serves thousands of clients at the same time. Each connection has the same destination port but a different source IP or source port, so the server's operating system can tell the connections apart. UDP, which has no connections, demultiplexes using only the destination IP and destination port.

The client's source port is normally chosen by the operating system from the ephemeral range. You never choose it for ordinary client programs. The reply from the server swaps the two ports, so it comes back to exactly the right socket.

Seeing it live

The program below runs several servers and clients on your own machine's loopback interface (127.0.0.1, which needs no real network). It asks the operating system for free ports with the special port number 0, shows that two programs cannot own the same TCP port, and shows how one server port serves three connections.

Node.js Ports, sockets and demultiplexing

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

All three connections share the server's single port, but the operating system gave each client its own ephemeral port, so the 4-tuples differ. The specific port numbers change from run to run, which is why the program prints facts about them instead of the numbers themselves. The EADDRINUSE error ("address already in use") is one you will meet in real life when you start a server twice, or restart one too quickly while the old connection is still closing.

Connection-oriented and connectionless service

The transport layer offers two styles of service, and the choice shapes everything an application does.

Connection-oriented (TCP) Connectionless (UDP)
Setup A handshake before data flows None; just send
Delivery Reliable, in order, no duplicates Best effort: loss, duplication and reordering possible
Unit of data A continuous byte stream with no message boundaries Separate datagrams with boundaries preserved
Control Flow control and congestion control None
Overhead 20-byte header at least, state in both hosts 8-byte header, no state
Typical uses Web, email, file transfer, databases DNS, voice and video, games, QUIC

Neither is better in general. Reliable ordered delivery is what most applications want, and TCP gives it for free, but it costs a handshake, waiting for retransmissions (a lost packet stalls everything after it) and extra state. UDP gives the application raw access to the network's behavior, and the application decides what to repair and what to ignore. A video call prefers to skip a lost frame rather than freeze the picture while it is retransmitted.

flowchart LR
    A["Application data"] --> T{"Which transport?"}
    T -- "needs every byte, in order" --> TCP["TCP<br/>stream, reliable"]
    T -- "timeliness or own rules" --> UDP["UDP<br/>datagrams, best effort"]
    TCP --> IP["IP"]
    UDP --> IP

Both transports use IP below them. Applications choose according to what they need.

The next lessons take both apart: UDP is simple enough to understand in one sitting, and TCP takes several lessons because of everything it does.

How the protocol is identified

When a packet arrives, how does the operating system know it is TCP and not UDP? The IPv4 header has a protocol field (and IPv6 has a "next header" field) that names the layer above: 6 means TCP, 17 means UDP and 1 means ICMP, as seen in the IPv4 packet lesson. The IP layer hands the payload to that protocol, which then reads the destination port and passes the data to the right socket. This is the same layered pattern as in the whole stack: each layer has a field that tells the layer below which one to use above.

A firewall rule such as "allow port 443" is not enough information on its own. The rule needs a protocol (TCP or UDP) too, because the two protocols have separate port spaces. HTTP/3, for example, uses UDP port 443, while HTTP/1.1 and HTTP/2 use TCP port 443.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. What do port numbers identify?

    IP addresses identify hosts; port numbers identify the application endpoint on the host. Together they form a socket address.
  2. 2. A web server listens on port 443 and has 1,000 clients connected. How does it tell the connections apart?

    All connections share the destination port, but the client IPs or client ephemeral ports differ, so each connection has a unique 4-tuple.
  3. 3. Which range holds the well-known ports?

    IANA reserves 0-1023 for well-known services such as HTTP (80), HTTPS (443), SSH (22) and DNS (53).
  4. 4. In the live example, why did listening on an already used TCP port fail with EADDRINUSE?

    The operating system refuses to give the same local TCP address and port to two listening sockets, because it could not tell where to deliver incoming connections.
  5. 5. Which statement about UDP is correct?

    UDP sends independent datagrams with best-effort delivery. It has no handshake, no retransmission and no ordering.

Summary

  • The transport layer delivers data between programs on two hosts, using port numbers on top of IP addresses.
  • A port is a 16-bit number: well-known 0-1023, registered 1024-49151, dynamic 49152-65535.
  • Multiplexing combines data from many applications; demultiplexing hands each arriving segment to the right socket.
  • A TCP connection is identified by its 4-tuple, which lets one server port serve many clients.
  • TCP is connection-oriented, reliable and stream-based; UDP is connectionless, best-effort and datagram-based.
  • The IP protocol field (6 for TCP, 17 for UDP) tells the network layer which transport protocol to hand data to.

Next, look at the simplest transport protocol in UDP.