SkillPilotAI

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.
UDP: no handshake, no ACK
  1. 1.PC 1 sends Datagram 1 to 192.168.88.10, UDP dst port 5000

  2. 2.The server receives it; no ACK is sent back

  3. 3.PC 1 sends Datagram 2; it is dropped at the switch

  4. 4.Nobody retransmits; the application must handle it if needed

PointTCPUDP
ConnectionConnection-oriented (3-way handshake)Connectionless
ReliabilityACK, retransmit, ordered deliveryNone (app's job)
Header20+ bytes8 bytes
SpeedSlower startFast, low overhead
Used byWeb, SSH, emailDNS, 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.
One IP, many applications
  1. 1.PC 1 -> 192.168.88.10, TCP dst port 443 (HTTPS)

  2. 2.Server kernel hands it to the web server (listening on 443)

  3. 3.PC 1 -> 192.168.88.10, UDP dst port 53 (DNS query)

  4. 4.Server kernel hands it to the DNS server, which replies

Port = the address of an application inside an IP

RangePortsExamples
Well-known0-1023SSH 22, HTTP 80, HTTPS 443
Registered1024-49151MySQL 3306, RDP 3389
Dynamic / ephemeral49152-65535Client side, temporary per connection
Linux default ephemeral32768-60999net.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.
Listening socket vs connected sockets
  1. 1.Server app starts: LISTEN 0.0.0.0:8080 (peer *:*)

  2. 2.PC 1 connects from ephemeral port 39576: new ESTAB socket

  3. 3.PC 1 connects again from 39592: another ESTAB socket

  4. 4.PC 2 connects from 35130: third ESTAB socket

  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.
Terminal
ss -tulpn
Output
$ 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.
Terminal
# srv 192.168.88.10ss -tan# pc1 (client side)ss -tncat /proc/sys/net/ipv4/ip_local_port_range
Output
srv$ 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	60999

68.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.
Terminal
# 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:8080
Output
IP 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 1

68.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.
Terminal
grep -wE '^(ssh|domain|http|https|ntp|bootps)' /etc/services
Output
ssh             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 service

68.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.
PowerShell
netstat -ano -p TCPnetstat -ano -p UDP
Output
> 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           *:*                                    792

68.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.