Skip to content

BeginnerLesson 10 of 11

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:

PortThe program behind it
80Web pages (HTTP)
443Web pages, locked so nobody on the way can read them (HTTPS)
53DNS
22SSH: logging in to another computer and typing commands on it
25Mail passed from one mail server to another (SMTP)
21FTP, 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.

Try itKnock on a server's ports
Your laptop192.168.1.20 Server 203.0.113.8022Remote login (SSH)25Mail delivery53DNS answers80Web pages443Web pages, locked

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.

CheckOne server at 203.0.113.80 runs a website and answers DNS questions. How does a DNS question reach the DNS program, and not the website?

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:

  1. 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.
  2. Every piece of data is numbered, and the receiver answers each piece with "got it".
  3. If a "got it" does not come back in time, the sender sends that piece again.
  4. 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.

Try itRace TCP against UDP
How lossy is the link?
Loss pattern 1
TCP checks every piece UDP just sends

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 forSent withWhy
Web pages, email, downloads, SSHTCPA page or a file with a missing piece is broken. Waiting a little is better.
Voice and video calls, online gamesUDPA piece that arrives late is useless, because the moment has passed. Skipping it is better.
DNSUDP, mostlyOne 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.

CheckYou are on a video call, and one packet of sound is lost. What is the better thing for the call to do?
CheckWhat does the TCP handshake do?

Discussion

Stuck, or found a mistake? Ask here. You sign in with GitHub.

Released under the Business Source License 1.1.