Ports and protocols
One device can run many programs that use the network at the same time, such as a website, a mail server, and a way to log in. Every packet carries a port number that says which program it is for. Programs also choose how they send. TCP checks that every piece arrives, and UDP just sends.
One address, many programs
An IP address gets a packet to the right device, the way a street address gets a letter to the right building. Inside the building, the letter still needs an apartment number to reach the right door. On a device, that number is the A number from 0 to 65535 in a packet that says which program on the device the packet is for, such as 443 for a web server. Often just called a port..
Here the comparison fails: a port is not a place inside the device. It is a number that a program asks the device for when it starts. The device then hands the program every packet with that number. For the programs everyone needs to find, the numbers are agreed in advance:
| Port | The program behind it |
|---|---|
| 80 | Web pages (HTTP) |
| 443 | Web pages, locked so nobody on the way can read them (HTTPS) |
| 53 | DNS |
| 22 | SSH: logging in to another computer and typing commands on it |
| 25 | Mail passed from one mail server to another (SMTP) |
| 21 | FTP, an old way of sending files that sends the password as plain text |
A port number is 16 bits, so it goes from 0 to 65535. An address and a port are written together with a colon. 203.0.113.80:443 is the locked web program on the server at 203.0.113.80.
Send a request to port 443, then 22, then to a port with nothing on it, such as 8080.
Your laptop's side has a port too. It is a free number the laptop picks for this one conversation, usually a high one, and new each time. The reply comes back to that number. So a reply finds the right program on your side. It also finds the right conversation when one program, such as your browser, has several open at once.
Two ways to send
Packets can be lost, or arrive out of order, as Messages in pieces showed. Some programs must get every piece. Others would rather skip a piece than wait for it. So there are two common A set of rules that two programs or devices agree on for how they talk to each other. for sending, and every packet uses one of them. Both carry port numbers.
TCP sets up a conversation:
- Before any data, the two sides send three short messages, called the handshake: "hello?", "hello!", and "OK". Now each side knows the other is there and ready.
- Every piece of data is numbered, and the receiver answers each piece with "got it".
- If a "got it" does not come back in time, the sender sends that piece again.
- The receiver hands the pieces to its program in order. It holds back any that arrive early until the gaps are filled.
UDP just sends. There is no handshake, no "got it", nothing sent again, and nothing put back in order. Each packet goes once. The program on the other side gets whatever arrives, in the order it arrives.
Squares are pieces of the message, one letter each. Round dots are TCP’s “got it” replies.
TCP: what the program gets
Not started
UDP: what the program gets
Not started
The laptop sends the word NETWORKING over TCP and over UDP, one letter per packet. Start with some loss, then try heavy loss and no loss. The same loss pattern always loses the same packets.
UDP is faster, because it never waits for a handshake or a "got it". But when packets are lost, its program gets a word with holes, sometimes in the wrong order. TCP takes longer with every lost piece, but its program always gets the whole word, in order.
Which one, when
| What it is for | Sent with | Why |
|---|---|---|
| Web pages, email, downloads, SSH | TCP | A page or a file with a missing piece is broken. Waiting a little is better. |
| Voice and video calls, online games | UDP | A piece that arrives late is useless, because the moment has passed. Skipping it is better. |
| DNS | UDP, mostly | One small question and one small answer. Asking again is quicker than a handshake. Very large answers use TCP. |
A newer way for the web
Some websites now send pages over UDP with The newest version of the rules browsers and web servers use to ask for and send pages. It runs on QUIC, over UDP.. HTTP/3 runs on A way of sending data over UDP that adds what UDP leaves out. It checks that every piece arrived, sends lost ones again, and encrypts the data. Browsers use it for HTTP/3., which adds its own checking, resending, and locking on top of UDP. Every piece must still arrive. These lessons follow the classic way, over TCP.