Ports & Sockets
One IP address holds many conversations at once. Ports say which program each packet is for, and sockets keep the conversations apart.
An interactive Networking lesson: 19 steps, about 30 minutes, on a live simulation in your browser.
pc2 has one address, 192.168.1.11, and two programs waiting for packets. A web server listens on TCP port 80. An echo service listens on UDP port 7. The port number is the part of the address that names the program.
Watch pc1 send the word hello to UDP port 7. There is no handshake and no connection: one datagram out, the same bytes echoed back. The command the engine prints is the one you would type: echo hello | nc -u -w1 pc2 7.
What you will learn
One address, many conversations
- One address, two services: An address names the machine. A port names the program on that machine that should get the packet.
- A conversation opens
- Two tabs, one server: Many conversations share one server port. They differ in the client's ephemeral port, picked fresh for each connection.
Ports with names
- Ports below 1024: Ports 0–1023 are well-known: the same number means the same service everywhere, and binding one needs root.
- Which bind needs root?
Ports without names
- The client's half: The client never chooses its port. Its kernel picks an unused ephemeral port for each connection.
- The ephemeral range: Ephemeral means picked by the kernel, used once, and returned. On Linux the pool defaults to 32768–60999.
- Drill: list the listeners
What a socket really is
- Five numbers, one conversation: A connection is a 5-tuple: protocol, two addresses, two ports. The listening socket accepts; each conversation gets its own socket.
- Break it: one port, one listener
- Drill: ask the port
When the port says no
- Saying goodbye with FIN: FIN means I am done sending, one per direction. The side that closes first keeps the connection in TIME_WAIT for 60 seconds.
- When is the port free again?
- Break it: the server stops
- What about the old connections?: Refused answers a SYN: nothing listens. Reset kills a conversation: the other end is gone. Both arrive as a RST.
- UDP never refuses the same way
- Three answers, back to open: Open means the program answered. Refused means the host answered. Timeout means nobody answered.
Recap & playground
- Cheat sheet
- Playground