Course contents

Unit 7: TCP, UDP and PortsLesson 7.2.1 (12 of 23 in this unit)61 of 104 in the Network Fundamentals course

Port numbers and sockets

Two clients reach the same server port at once. How can it send each reply to the right conversation? Let's read both endpoint addresses and ports together.

Beginner · 7 min read

Covers Port ranges, Source vs. destination port and Sockets.

Technical definition

Port number is a 16-bit field in a transport header, used to identify an application endpoint within TCP or UDP. A TCP connection is identified by its two endpoint address-and-port pairs; TCP and UDP have separate port namespaces. Port 0 has special or reserved uses rather than being an ordinary service port.

Three browser tabs, one server port

Two people in the same office open the same website at the same moment. The web server sees two connections, both to its port 443. Even one person with three browser tabs can open three connections to that same port. How does the server keep all of these apart and send each reply to the right tab?

IP finds the host, the port finds the application

One computer runs many programs that use the network at the same time: a browser, an email app, a chat app and a game. They all share the same IP address, so delivery happens in two steps:

Diagram
IP finds the host, the port finds the application
IP finds the host, the port finds the application
AddressLayerIdentifiesExample
IP addressNetwork (Layer 3)Which host (device) the packet is for203.0.113.10
Port numberTransport (Layer 4)Which application on that host gets the data443 (the web server program)
Read each row across: the left box names the item, the others describe it.

Think of the IP address as the street address of a block of flats and the port number as the flat number. The postman needs the street address to find the building and the flat number to find the right person inside.

Source port and destination port

Diagram
The TCP conversation ends at the applications, not at R1
The TCP conversation ends at the applications, not at R1
The TCP conversation ends at the applications, not at R1PC-1 Ethernet at 192.168.1.20/24 connects through SW1 to R1 Gi0/0 at 192.168.1.1/24. R1 Gi0/1 at 203.0.113.1/24 connects to a server at 203.0.113.10/24. The example TCP connection is 192.168.1.20 port 52000 to 203.0.113.10 port 443. Replies reverse the endpoint addresses and ports. No NAT or TCP proxy is present.PC-1SW1R1ServerEth0 → Gi0/1Gi0/24Gi0/0Gi0/1Eth0192.168.1.20/24192.168.1.1/24203.0.113.1/24203.0.113.10/24Request: TCP 192.168.1.20:52000 → 203.0.113.10:443Reply: TCP 203.0.113.10:443 → 192.168.1.20:52000Gi0/0 is a physical interface name; TCP 443 is a transport-layer port number.

On small screens, swipe sideways or focus the diagram and use the arrow keys.

A routed lab with documentation addresses, no NAT and no TCP proxy. SW1 forwards frames, R1 routes packets, and the two endpoint TCP stacks maintain the connection. Both endpoints need a return route.

Read the outgoing connection as TCP, local 192.168.1.20:52000, remote 203.0.113.10:443. The reply swaps those endpoints; it does not choose a new destination service port on the laptop. Another simultaneous connection from local port 52001 has a different endpoint pair even if it reaches the same server.

TCP 443 and UDP 443 are different transport endpoints, even though the number is the same. A firewall rule allowing only one does not automatically allow the other. Also, browser tabs need not map one-to-one to TCP connections: modern web protocols can reuse or multiplex conversations. The addresses and ports in a capture describe connections, not a guaranteed count of open tabs.

A port number is a 16-bit number (0 to 65,535) in the TCP or UDP header. Every TCP segment and UDP datagram carries two of them:

  • Destination port: which service the data is for, such as 443 for HTTPS.
  • Source port: which program on the sender the data came from. The client's operating system normally picks an unused high number, often at random. This temporary number is called an ephemeral port.

When the server replies, it swaps the two: the source port becomes the destination port, and the destination port becomes the source port.

Diagram
Ports swap on the reply
Ports swap on the reply
ClientIP 192.168.1.20RouterWeb serverIP 203.0.113.10

Device key: Client (laptop)

Lines show the example’s network connections. Thick links: uplinks/WAN, as labelled. The coloured path and envelope follow the current step; its text identifies the sender, receiver and traffic type.

Reading the addresses

IP labels identify interfaces or destinations. Subnet labels identify networks; /n is the prefix length. A router ID (RID) identifies a routing process and need not be a reachable interface.

An IP address alone is not a URL. A web URL includes a scheme such as https://; an address on a router or printer does not imply a website.

  • Client: IP 192.168.1.20
  • Web server: IP 203.0.113.10
  1. 1. Request: source port 52000 (picked by the client), destination port 443 (HTTPS).
  2. 2. Reply: the server swaps them: source 443, destination 52000. The reply reaches the right browser tab.

This example ignores NAT. In a real home network, the router's NAT would also change the client's source address, and often its source port, on the way out, then reverse the change on the reply.

The three port ranges

IANA (the Internet Assigned Numbers Authority), the organisation that assigns internet numbers, divides ports into three ranges:

Diagram
The three port ranges
The three port ranges
RangeNameUsed for
0–1023Well-known (system) portsCommon server services: HTTP 80, HTTPS 443, SSH 22, DNS 53
1024–49151Registered (user) portsServices registered by companies or projects, such as RDP 3389
49152–65535Dynamic (private, ephemeral) portsTemporary source ports picked by clients
Read each row across: the left box names the item, the others describe it.

Real operating systems do not always follow the last range exactly. Windows (since Vista) and macOS use the IANA range 49152–65535, but most Linux systems pick source ports from 32768–60999 by default, and administrators can change these ranges. The idea is the same everywhere: an ephemeral port is a short-lived, high number that is used for one connection and freed when it closes.

Diagram
The three port ranges
The three port ranges
Source portDestination port
Client → server (request)Ephemeral, e.g. 52000Well-known, e.g. 443
Server → client (reply)Well-known, 443Ephemeral, 52000
Read each row across: the left box names the item, the others describe it.

Sockets: four values name a connection

An IP address plus a port number is called a socket, for example 203.0.113.10:443. A connection has a socket at each end, so it is identified by four values, called the socket pair:

ConnectionSource IPSource portDestination IPDestination port
Tab 1192.168.1.2052000203.0.113.10443
Tab 2192.168.1.2052001203.0.113.10443
Colleague192.168.1.3152000203.0.113.10443

Every row differs in at least one value, so the server can keep the connections apart. The colleague even uses the same source port, 52000, but the source IP address is different, so it is still a separate connection. These four values plus the protocol (TCP or UDP) are often called the 5-tuple. Firewalls and NAT devices use it to track traffic.

Why servers use fixed ports and clients random ones

Fixed, well-known ports mean a client knows by default where to find a service: a browser does not have to ask which port HTTPS uses. Different source ports give each new connection its own unique socket pair, so many connections to the same service never get mixed up. Choosing source ports at random also makes it harder for an attacker to guess and fake (spoof) a connection.

In practice, when a firewall rule "doesn't work", a common cause is a rule written for the wrong side: it matches the service port as the source port instead of the destination port.

Spotting socket pairs in show tcp brief

On a Cisco router, show tcp brief lists the TCP connections that end on the router itself, such as SSH sessions. The output below is based on Cisco documentation, not captured from a lab device.

Example output · based on Cisco documentation; exact format varies by platform and software version
R1#show tcp brief
TCB       Local Address               Foreign Address             (state)
6523A4FC  192.168.1.1.22              192.168.1.50.51544          ESTAB
65239A84  192.168.1.1.22              192.168.1.51.60212          ESTAB
What to look for: Cisco writes a socket as address.port, so 192.168.1.1.22 means port 22 on 192.168.1.1. Both sessions use the router's SSH port 22 in Local Address, but each Foreign Address is a different client socket, so they are two separate connections. ESTAB means each connection is established.

Test yourself on ports and sockets

Predict · scenario 1

Your laptop opens an SSH session, sending a segment from source port 50500 to destination port 22 on a server. Which ports does the server's reply use?

Predict · scenario 2

A firewall log shows incoming connections to TCP port 3389 (RDP). Which port range is that?

Predict · scenario 3

Two browser tabs on the same PC are both connected to 203.0.113.10:443. Which values together identify each TCP connection?

Technical references

RFC 6335: port ranges and assignment; IANA service/port registry; RFC 9293: base TCP header, connection state, sequence space and delivery.