UDP, ports and sockets
One server IP can run thousands of conversations at once because of ports and sockets. This topic compares TCP with UDP, explains port ranges, and shows the difference between a listening socket and the connected sockets it creates.
68.1 TCP vs UDP: how they work
- TCP: SYN, SYN-ACK, ACK first, then data; every segment is acknowledged, so it is reliable but slower to start.
- UDP: no handshake and no ACK; the sender just sends datagrams one after another.
- A lost UDP datagram is not retransmitted: that job belongs to the application.
- UDP's header is only 8 bytes, so it is very light and fast.
- Step 1
1.PC 1 sends Datagram 1 to 192.168.88.10, UDP dst port 5000
- Step 2
2.The server receives it; no ACK is sent back
- Step 3
3.PC 1 sends Datagram 2; it is dropped at the switch
- Step 4
4.Nobody retransmits; the application must handle it if needed
| Point | TCP | UDP |
|---|---|---|
| Connection | Connection-oriented (3-way handshake) | Connectionless |
| Reliability | ACK, retransmit, ordered delivery | None (app's job) |
| Header | 20+ bytes | 8 bytes |
| Speed | Slower start | Fast, low overhead |
| Used by | Web, SSH, email | DNS, video calls, gaming, QUIC |
68.2 Ports: the address of an application
- A web server and a DNS server can run on the same IP; the 16-bit port number tells which app gets the packet.
- HTTPS request -> TCP dst port 443 -> web server.
- DNS query -> UDP dst port 53 -> DNS server.
- Step 1
1.PC 1 -> 192.168.88.10, TCP dst port 443 (HTTPS)
- Step 2
2.Server kernel hands it to the web server (listening on 443)
- Step 3
3.PC 1 -> 192.168.88.10, UDP dst port 53 (DNS query)
- Step 4
4.Server kernel hands it to the DNS server, which replies
Port = the address of an application inside an IP
| Range | Ports | Examples |
|---|---|---|
| Well-known | 0-1023 | SSH 22, HTTP 80, HTTPS 443 |
| Registered | 1024-49151 | MySQL 3306, RDP 3389 |
| Dynamic / ephemeral | 49152-65535 | Client side, temporary per connection |
| Linux default ephemeral | 32768-60999 | net.ipv4.ip_local_port_range |
68.3 Sockets: listening vs connected
- Socket = IP address + port (+ protocol).
- The server app opens one listening socket on 8080 that only waits for new connections.
- Each accepted connection gets a new connected socket, all with the same local port 8080.
- The client OS picks an ephemeral source port for each connection.
- Each connection is unique by its 4-tuple (src IP, src port, dst IP, dst port); add protocol for the 5-tuple.
- Step 1
1.Server app starts: LISTEN 0.0.0.0:8080 (peer *:*)
- Step 2
2.PC 1 connects from ephemeral port 39576: new ESTAB socket
- Step 3
3.PC 1 connects again from 39592: another ESTAB socket
- Step 4
4.PC 2 connects from 35130: third ESTAB socket
- Step 5
5.All on local port 8080; each 4-tuple is unique
68.4 Practical: ss -tulpn
- Lab: socat apps on server 192.168.88.10, TCP 8080 and UDP 5000.
- -t TCP, -u UDP, -l listening, -p process, -n numeric.
- TCP shows LISTEN; UDP shows UNCONN because UDP has no connection.
ss -tulpn$ ss -tulpnNetid State Recv-Q Send-Q Local Address:Port Peer Address:PortProcessudp UNCONN 0 0 0.0.0.0:5000 0.0.0.0:* users:(("socat",pid=26,fd=5))tcp LISTEN 0 5 0.0.0.0:8080 0.0.0.0:* users:(("socat",pid=20,fd=5))68.5 Practical: listening vs connected in ss
- Server: one LISTEN line and three ESTAB lines, all on local port 8080.
- PC 1: the same connections from local ports 39576 and 39592.
- ip_local_port_range shows Linux picks ephemeral ports from 32768 to 60999.
# srv 192.168.88.10ss -tan# pc1 (client side)ss -tncat /proc/sys/net/ipv4/ip_local_port_rangesrv$ ss -tanState Recv-Q Send-Q Local Address:Port Peer Address:PortLISTEN 0 5 0.0.0.0:8080 0.0.0.0:*ESTAB 0 0 192.168.88.10:8080 192.168.88.21:39592ESTAB 0 0 192.168.88.10:8080 192.168.88.21:39576ESTAB 0 0 192.168.88.10:8080 192.168.88.22:35130 pc1$ ss -tnState Recv-Q Send-Q Local Address:Port Peer Address:PortESTAB 0 0 192.168.88.21:39576 192.168.88.10:8080ESTAB 0 0 192.168.88.21:39592 192.168.88.10:8080pc1$ cat /proc/sys/net/ipv4/ip_local_port_range32768 6099968.6 Practical: UDP vs TCP on the wire
- UDP: a single line, one 3-byte datagram.
- TCP: [S], [S.], [.] handshake first, then data with [P.].
- Both times PC 2's source port is a random high number: the ephemeral port.
# srvtcpdump -nn -t -i eth0 'udp port 5000 or tcp port 8080'# pc2echo hi | socat - UDP:192.168.88.10:5000echo hi | socat - TCP:192.168.88.10:8080IP 192.168.88.22.35771 > 192.168.88.10.5000: UDP, length 3IP 192.168.88.22.32804 > 192.168.88.10.8080: Flags [S], seq 1103925608IP 192.168.88.10.8080 > 192.168.88.22.32804: Flags [S.], seq 918387496IP 192.168.88.22.32804 > 192.168.88.10.8080: Flags [.], ack 1IP 192.168.88.22.32804 > 192.168.88.10.8080: Flags [P.], seq 1:4, ack 168.7 Which service uses which port
- /etc/services maps names to ports: SSH 22/tcp, DNS 53 on tcp and udp, DHCP (bootps) 67/udp.
- HTTPS 443 is on TCP and on UDP (HTTP/3 over QUIC); 8080 is registered as http-alt.
grep -wE '^(ssh|domain|http|https|ntp|bootps)' /etc/servicesssh 22/tcp # SSH Remote Login Protocoldomain 53/tcp # Domain Name Serverdomain 53/udpbootps 67/udphttp 80/tcp www # WorldWideWeb HTTPntp 123/udp # Network Time Protocolhttps 443/tcp # http protocol over TLS/SSLhttps 443/udp # HTTP/3http-alt 8080/tcp webcache # WWW caching service68.8 Windows: netstat -ano
- -a all, -n numeric, -o owning PID.
- 135 and 445 LISTENING are Windows RPC and file sharing.
- UDP lines have an empty State column: UDP has no LISTENING/ESTABLISHED states.
netstat -ano -p TCPnetstat -ano -p UDP> netstat -ano -p TCP TCP 0.0.0.0:135 0.0.0.0:0 LISTENING 1544 TCP 0.0.0.0:445 0.0.0.0:0 LISTENING 4 TCP 0.0.0.0:5040 0.0.0.0:0 LISTENING 1868> netstat -ano -p UDP UDP 0.0.0.0:123 *:* 1828 UDP 0.0.0.0:5353 *:* 792 UDP 0.0.0.0:5355 *:* 79268.9 Common mistakes
- Thinking UDP is useless because it is unreliable: DNS, calls and games use it on purpose (a late voice packet is useless anyway).
- QUIC (HTTP/3) adds reliability and encryption on top of UDP.
- Thinking a server opens a new port per client: the local port stays the same (443 or 8080).
- Expecting a LISTEN state for UDP sockets: ss shows UNCONN.
68.10 Interview tips
- TCP = connection-oriented, reliable, ordered (handshake, ACK, retransmit); UDP = connectionless, 8-byte header, fast for real-time apps.
- A connection is identified by the 5-tuple: protocol + src IP:port + dst IP:port.
- 1000 clients on 443 = one listening port, 1000 connected sockets with different 4-tuples.
- Linux: ss -tulpn; Windows: netstat -ano.