TCP, UDP and port numbers

8 min 5 sections 3 practice questions 13 sources cited

Once a message reaches the right computer it still has to reach the right program, and the sender has to decide whether to check that it arrived. Two transport protocols do this work: TCP confirms that everything arrived and resends what is lost. UDP sends and does not confirm. Port numbers name the program at each end. No background is assumed. Firewall rules, scan reports and most network attacks are described in these terms.

By the end of this unit you will be able to:

  • Tell apart TCP and UDP, and choose which suits a given kind of traffic
  • Put the three steps of the TCP handshake in order and say what each one does
  • Explain what a flag, a sequence number and the window are for
  • Explain what a port and a socket are, and name the three ranges of port numbers
  • Name the well-known port for each common service

A call that lost three words and a download that lost nothing

An illustrative example

At home, Maya is on a video call with her sister while her laptop downloads a large folder of holiday photographs. For a moment the Wi-Fi falters. The picture freezes, three or four words are lost, and the call carries on as if nothing had happened.

The download pauses too. When it finishes, every photograph opens perfectly. Not one is damaged or missing a corner.

Why did the network replace every missing piece of the photographs and none of the missing words?

Two ways to deliver: confirm everything, or just send

The , layer 4, carries data between two programs. It offers two services, and the program chooses between them.

The Transmission Control Protocol (TCP) is like recorded delivery. The two ends first agree to talk. Every piece is numbered, the receiver confirms what arrived, and anything missing is sent again. is connection-oriented and reliable.

The User Datagram Protocol (UDP) is like dropping postcards in a postbox. Each one is sent on its own, with no agreement beforehand, no confirmation and no resending. is connectionless, and its delivery is described as best effort.

TCP and UDP side by side
PointTCPUDP
Before sendingSets up a connection with a handshakeNothing: it just sends
Lost dataDetected and sent againStays lost unless the program itself asks again
OrderDelivered in the order sentMay arrive in any order
CostMore delay and a larger headerLittle delay and a small header
Unit of dataSegment
Typical usesWeb pages, email, file transfer, remote loginName lookups, voice and video calls, handing out addresses
Protocol number in the IP header617

Maya's call and her download

The call used UDP. A word that arrives a second late is useless, so nothing is sent again and the call moves on. The download used TCP. Getting every byte mattered more than speed, so each lost piece was sent again.

The three-way handshake opens a TCP connection

Before any data moves, TCP runs a three-way handshake. Each message carries flags: single bits in the TCP header that mark what the message is for.

  1. The client sends a segment with the SYN flag set. SYN is short for synchronise. The segment carries the client's starting sequence number.
  2. The server replies with SYN and ACK both set. ACK is short for acknowledge. The reply confirms the client's number and gives the server's own.
  3. The client sends an ACK that confirms the server's number. The connection is now open in both directions.

How TCP opens a connection in three steps

How TCP opens a connection in three stepsTwo columns, client on the left and server on the right, with time running down. SYN crosses to the server, SYN-ACK crosses back, ACK crosses to the server. The server's half-open state, between SYN and ACK, is marked as the risk.ClientClosedSYN sentOpenServerListeningHalf-openfloods abuse itOpenSYNSYN-ACKACKHow TCP opens a connection in three stepsTwo columns, client on the left and server on the right, with time running down. SYN crosses to the server, SYN-ACK crosses back, ACK crosses to the server. The server's half-open state, between SYN and ACK, is marked as the risk.ClientClosedSYN sentOpenServerListeningHalf-openfloods abuse itOpenSYNSYN-ACKACK
Read this figure as text

Before any data moves, TCP runs a three-way handshake between a client and a server. Each message is marked by flags, single bits in the TCP header. The figure shows the three messages crossing in order, with time running down, and the state each end is in.

  1. At the start the client's connection is closed and the server is listening for callers.
  2. SYN: the client sends a segment with the SYN flag set, short for synchronise, carrying its starting sequence number. The client is now in the SYN sent state. On receiving it the server enters the half-open state.
  3. SYN-ACK: the server replies with SYN and ACK both set, ACK being short for acknowledge, confirming the client's number and giving its own. On receiving it the client's connection is open.
  4. Half-open: from the SYN until the ACK arrives, the server holds memory for a caller who has not yet confirmed. This waiting state is what flooding attacks on TCP abuse, which is why it is marked as the risk.
  5. ACK: the client sends an ACK that confirms the server's number. On receiving it the server's connection is open too, so it is open in both directions.
  6. Closing takes four steps, a FIN and its acknowledgement from each side. A segment with the RST flag, for reset, ends a connection at once.

The short form is SYN, SYN-ACK, ACK. After the second step the server holds a half-open connection: it has set aside memory for a caller who has not yet confirmed. That waiting state is what flooding attacks on TCP abuse.

Closing takes four steps. One side sends FIN, for finish, and the other acknowledges it. Then the second side sends its own FIN and receives an acknowledgement. A segment with the RST flag, for reset, ends a connection at once with no exchange.

Flags, sequence numbers and the window

SYN
Synchronise. Opens a connection and carries the starting sequence number.
ACK
Acknowledge. Confirms what has been received. Set on almost every segment after the first.
FIN
Finish. The sender has no more data and wishes to close.
RST
Reset. Ends the connection immediately, for example when no program is listening on the port.
PSH
Push. Asks the receiver to pass the data to the program without waiting for more.
URG
Urgent. Marks part of the data as needing attention first.

These six are the classic flags. Two later ones, ECE and CWR, signal congestion on the network.

TCP numbers every byte it sends. The sequence number says where a segment's data belongs in the stream. The acknowledgement number tells the sender which byte the receiver expects next. A gap in the acknowledgements shows that something was lost, and the sender sends it again.

Starting sequence numbers are chosen to be hard to predict. If an outsider could guess them, forged segments could be slipped into someone else's connection.

The window is the amount of data the receiver is ready to accept. A busy receiver advertises a smaller window and the sender slows down. This is flow control.

A port names the program; a socket names one end

An IP address brings a packet to the right computer. A port brings it to the right program on that computer. A port is a number from 0 to 65,535 carried in the transport header. TCP and UDP each have their own set of ports.

A program that offers a service listens on a known port: a web server on TCP port 443, for example. The program that calls it picks a spare high-numbered port for its own end, so that the reply can find its way back.

An IP address and a port together are a socket, written as 203.0.113.20:443. One TCP connection is a pair of sockets, one at each end, and that pair is what tells two conversations apart.

The three ranges of port numbers
RangeNameUsed for
0 to 1,023Well-known, or system, portsStandard services such as web, mail and name lookup
1,024 to 49,151Registered, or user, portsParticular products, by registration
49,152 to 65,535Dynamic, private or The temporary port a calling program picks for itself

The port numbers to know by heart

rules, scan reports and log entries name a service by its port number alone. These are the numbers met most often. Unless UDP is stated, the service uses TCP.

Web, mail, files and network housekeeping
PortServiceWhat it does
20, 21 and 990FTP and FTPSFile Transfer Protocol: 21 carries the commands and 20 the files. FTPS sends the commands on 990, with TLS, the encryption used for web pages
22SSHSecure Shell: encrypted remote login and file copy
23TelnetRemote login with nothing encrypted
25, 465 and 587SMTPSimple Mail Transfer Protocol: passes email between servers on 25. A user's mail program sends on 465, with TLS, or on 587
53DNSDomain Name System: turns names into addresses. UDP for lookups, TCP for large transfers
67 and 68DHCPDynamic Host Configuration Protocol: hands out addresses. UDP
69TFTPTrivial File Transfer Protocol: simple file copy with no login. UDP
80HTTPWeb pages, not encrypted
110 and 995POP3 and POP3SPost Office Protocol: collects email from a server. POP3S on 995 adds TLS
123NTPNetwork Time Protocol: keeps clocks in step. UDP
143 and 993IMAP and IMAPSInternet Message Access Protocol: reads email kept on a server. IMAPS on 993 adds TLS
443HTTPSWeb pages, encrypted
Sign-in, directories, management and remote access
PortServiceWhat it does
49TACACS+Checks administrators who sign in to network devices
88KerberosSign-in service that issues tickets. TCP and UDP
161 and 162SNMPSimple Network Management Protocol: monitors devices. UDP
389LDAPLightweight Directory Access Protocol: looks up people and devices in a directory
445SMBServer Message Block: Windows file sharing
500 and 4500IKEInternet Key Exchange: agrees the keys for IPsec, which encrypts packets. UDP
514SyslogSends log messages to a central collector. UDP
636LDAPSLDAP, encrypted
1433SQL ServerMicrosoft's database service
1701 and 1723L2TP and PPTPTwo older ways to make a private link across the internet: L2TP on UDP 1701, PPTP on TCP 1723
1812 and 1813RADIUSChecks users who join a network and records their use. UDP
3389RDPRemote Desktop Protocol: a Windows screen shown over the network

A question, worked through

A regional council's firewall administrator proposes four changes to reduce exposure. The security manager must approve the list. Staff must still be able to browse the web. The council has no remote workers and no outside suppliers who sign in to its systems.

Which proposed change would MOST disrupt the council's work?

  1. Blocking UDP port 53 outbound from the council's name server
  2. Blocking TCP port 23 between the council's internal servers
  3. Blocking TCP port 21 inbound from the internet to all servers
  4. Blocking TCP port 3389 inbound from the internet to all servers
Decide on your answer, then open the reasoning
  1. Each option is a port, so translate first: 53 is DNS, 23 is Telnet, 21 is FTP and 3389 is Remote Desktop.
  2. Rule out c and d: nobody outside needs to sign in or send files, so inbound FTP and Remote Desktop from the internet meet no stated need. Closing them removes exposure and disrupts nothing.
  3. Between a and b: Telnet inside the network may still be used by an administrator, so blocking it could inconvenience someone. It sends passwords unencrypted, and SSH on port 22 does the same job.
  4. Blocking outbound DNS from the name server stops every name lookup. No website opens by name, which breaks the one thing the scenario says must keep working. The answer is a.

Answer: A. Blocking UDP port 53 outbound from the council's name server

Choosing between good answers

The traffic must arrive complete and in order, such as a web page, an email or a file
expect TCP. When speed matters more than completeness, such as voice, video or a quick lookup, expect UDP.
A question gives only a port number
turn it into a service before weighing the options: 22 is SSH, 53 is DNS, 443 is HTTPS.
DNS appears with TCP in place of UDP
think of a copy of a whole zone of names, or an answer too large for UDP, and not of an ordinary lookup.
A server shows many connections stuck after the SYN-ACK
the handshakes are not being completed. That pattern is a flood of forged connection requests, not a busy day.

Try it

Read a scan report for one web server and say what open, closed and filtered each prove about the port and about the path to it.

Loading the preview.

Check your understanding

3 questions on this unit, easiest first. Confirm an answer to see the reasoning for it, then the right answer, the other options and the rule to reuse. Your answers are kept in this browser and feed readiness and review.

These questions are drafts that no named reviewer has approved yet.

Question 1 of 3

A trainee analyst is reading a firewall log that names services only by TCP port. Match each port to the service that uses it.

Choose the match for each entry. Every match has to be right.

What to remember

  • TCP sets up a connection, numbers every byte and resends what is lost. UDP sends single datagrams with no set-up, no confirmation and no resending.
  • The TCP handshake is SYN, SYN-ACK, ACK. A normal close uses FIN and ACK in each direction. RST ends a connection at once.
  • The six classic flags are SYN, ACK, FIN, RST, PSH and URG.
  • A port names a program, and an IP address with a port is a socket. Ports 0 to 1,023 are well-known, 1,024 to 49,151 registered and 49,152 to 65,535 dynamic.
  • Common ports: 20 and 21 FTP, 22 SSH, 23 Telnet, 25 SMTP, 53 DNS, 80 HTTP, 88 Kerberos, 110 POP3, 143 IMAP, 389 LDAP, 443 HTTPS, 636 LDAPS, 3389 RDP.
  • Their TLS forms: 465 and 587 mail submission, 990 FTPS, 993 IMAPS, 995 POP3S.

Say it from memory

  1. How does TCP differ from UDP in setting up, lost data and order, and what protocol number does each carry in the IP header?

    TCP is connection-oriented: it sets up a connection with a handshake, sends lost data again and delivers in the order sent. UDP is connectionless: it just sends, lost data stays lost and datagrams may arrive in any order. TCP is protocol 6 and UDP is protocol 17.

    Read this part again

  2. What are the three steps of the TCP handshake, and what is a half-open connection?

    The client sends SYN with its starting sequence number. The server replies with SYN and ACK. The client sends ACK, and the connection is open. After the second step the server holds a half-open connection, with memory set aside for a caller who has not confirmed, which flooding attacks abuse.

    Read this part again

  3. What do the RST and FIN flags do, and what is the TCP window for?

    FIN, for finish, means the sender has no more data and wishes to close. RST, for reset, ends the connection immediately, for example when no program is listening on the port. The window is the amount of data the receiver is ready to accept; a smaller window slows the sender, which is flow control.

    Read this part again

  4. What is a socket, and what are the three ranges of port numbers?

    A socket is an IP address and a port together, such as 203.0.113.20:443. Ports 0 to 1,023 are well-known, or system, ports. Ports 1,024 to 49,151 are registered, or user, ports. Ports 49,152 to 65,535 are dynamic, private or ephemeral ports.

    Read this part again

  5. Which services use ports 22, 23, 53, 389, 445 and 3389?

    Port 22 is SSH, encrypted remote login. Port 23 is Telnet, remote login with nothing encrypted. Port 53 is DNS. Port 389 is LDAP, and 636 is LDAPS. Port 445 is SMB, Windows file sharing. Port 3389 is RDP, the Remote Desktop Protocol.

    Read this part again

Sources