TCP and UDP · Introduction · Module 1
Ports: how one host keeps its conversations apart
Show topologyHide topology
The router R1 at the left end and the host H1 at the right end, joined by one cable. At R1’s end, Et0/0 above the cable and .1 under it: R1’s interface Ethernet0/0 with address 10.0.1.1. At H1’s end, eth0 above and .10 under it: H1’s interface eth0 with address 10.0.1.10. Under the cable, mid-way, its network: 10.0.1.0/24. A legend explains the short forms.
What a port is
One address, many programs
The router R1 sends data to one address, 10.0.1.10, on the host H1, for two programs. How does each piece reach the right one?
The header of each packet holds the address the packet goes to, and that address brings the data across the link to H1. But H1 runs several programs at once, each sending and receiving its own data. An address leads to an interface of a host, not to one program, so something else has to pick the program inside H1.
That something is a number. Each of the two programs on H1 waits for data at a numbered place on the host, a port, and that number is its port number. So data sent to a program carries two values, the host’s address and the program’s port number. The address brings the data to H1, and H1 hands it to the program on the port the data names. In Figure 1.1, data for port 2001 reaches that program only, and the program on port 2002 receives nothing.
A cable runs down from the router R1 to the host H1. Beside it, an arrow points down, next to a tag for data sent to address 10.0.1.10, port 2001. Inside H1 sit two programs, one on port 2001 and one on port 2002. A second arrow inside H1 ends on the program on port 2001 only; the program on port 2002 is dimmed.
On a switch, a port is an interface, the place where a cable plugs in. A port number names a program on a host instead, and one cable can carry data for many port numbers at once.
Key idea
The address brings the data to the host, through one of its interfaces. The port number finds the program.
How it works
One side waits, one side connects
Who opens and who waits
The programs on H1 wait on their port numbers. So who starts a conversation?
The program that knows where to send its data starts the conversation. A program that others will contact does not know who will come or when, so it waits on its port number. A program that waits on a port number the others know in advance is a server.
The other program is given the server’s address and port number, so it knows where to start. A client is a program that starts a conversation by contacting a server’s address and port number. The browser on your phone or computer is a client in this sense.
The reply travels from the server back to the client, so it needs a number to go to as well. Therefore the client’s own end of the conversation has a port number too, and the replies come back to it.
R1 is a router, and it also runs programs of its own. A program on R1 starts both conversations with H1, so it is the client of both; for short, R1 is the client. The programs on H1 are the servers (Figure 1.2). On R1, the client’s number is chosen for that one conversation and differs from the server’s number. R1’s end of the conversation with port 2002, for example, is port 45525.
R1 stands on the left, tagged “client · starts”, and H1 on the right, tagged “server · waits”, joined by one link. Beside the link a strong arrow marked “starts” points from R1 to H1, and an arrow marked “reply” points back. Under R1 a tag reads “own number · 45525”; under H1 a tag reads “port 2002”.
Key idea
Each end of a conversation has its own port number.
How the numbers travel
Each piece of data has to carry both port numbers. Where do they travel?
They travel in headers. TCP is a set of rules that two programs follow to carry data between them. It puts the program’s data into a segment, whose header holds the two port numbers, “from” for the sender’s end and “to” for the receiver’s end. The segment travels inside a packet, whose header holds the two addresses (Figure 1.3).
Before it sends, a client often wants to know that a program waits at the other end. So with TCP, the two programs first agree that data will flow between their ends. That agreement is a connection, and with TCP each conversation between two programs is one connection.
Step 1 of 7
A program on R1 has data for the program on H1 port 2001. The two already have a connection open. R1’s end of it is port 32652.
Step 2 of 7
TCP puts the data in a segment. Its header says from port 32652, to port 2001.
Step 3 of 7
The segment travels inside a packet. Its header says from 10.0.1.1, to 10.0.1.10. The packet crosses the link.
Step 4 of 7
The address 10.0.1.10 brings the packet to H1. H1 takes the segment out.
Step 5 of 7
H1 reads “to port 2001” and hands the data to that program. The program on 2002 receives nothing.
Step 6 of 7
The reply travels back up the link and swaps both pairs: from port 2001 to port 32652, from 10.0.1.10 to 10.0.1.1. R1 hands it to its program on 32652.
Step 7 of 7
R1 has a second connection, from port 45525 to the program on port 2002. The two connections share both addresses, but their ports differ.
R1 sits at the top left with its end, port 32652, beside it. A link runs straight down into H1, which holds two programs, on ports 2001 and 2002. A segment from 32652 to 2001 travels down beside the link inside a packet from 10.0.1.1 to 10.0.1.10, with an arrow beside the link pointing down; inside H1 the segment is handed to the program on 2001. The reply travels back up beside the link with both pairs swapped, to R1’s end 32652. The last step adds R1’s second end, 45525, and shows its two connections, 32652 with 2001 and 45525 with 2002, each a dashed line from R1’s end down to its program, beside the link.
A connection is not a cable, so one link carries both of R1’s connections with H1. Each connection is known by the address and the port at both of its ends.
Not every program needs a connection. A program that sends only short messages can use UDP, another set of rules, which also carries both port numbers but opens no connection first, so a short message leaves at once.
On R1, the command telnet, followed by an address and a port, makes R1 a client and asks for a connection to that port. show tcp brief then lists R1’s connections, one line each. The text a device prints in reply to a command is its output.
On R1, the client command:
R1 is the client: it starts a connection to H1’s address and to port 2001, where a program waits.
1R1#telnet 10.0.1.10 20012Trying 10.0.1.10, 2001 ... Open
Scroll sideways to see the whole output
- The command:
telnet, then H1’s address, then the port to connect to. Open: the connection is open.
On R1, right after:
Now R1 lists one open connection: its own end, then H1’s end at port 2001.
R1#show tcp brief1Local Address Foreign Address (state)210.0.1.1.54329 10.0.1.10.2001 ESTAB30.0.0.0.22 *.* LISTEN3::.22 *.* LISTEN
Scroll sideways to see the whole output
- Column names: R1’s end (
Local Address), the far end (Foreign Address), the state. - R1’s end is address 10.0.1.1, then port 54329, a number chosen for this connection.
ESTABmeans the connection is open. - R1 itself waiting on port 22.
Full output · 4 lines
R1#show tcp briefTCB Local Address Foreign Address (state)7972E9216AB8 10.0.1.1.54329 10.0.1.10.2001 ESTAB------------ 0.0.0.0.22 *.* LISTEN------------ ::.22 *.* LISTEN
Scroll sideways to see the whole output
Key idea
A connection is known by both ends: an address and a port at each end. A reply swaps the two.
Use the rule: a reply swaps the two ends of its own connection.
Verify
Read R1’s list of connections
When R1 holds two connections with H1, one to each program, how does R1 show them, and where is each one?
show tcp brief lists the TCP connections of the device you are on, one line for each connection (Figure 1.4). R1 prints each end as an address, a dot and a port, so in 10.0.1.10.2002 the last number is the port.
Which end opened the connection? H1’s end is at 2002, the port R1 was told to connect to, so it is the server. R1’s end, 45525, was typed by no one. It is R1’s own number for this connection, so R1 is the client that opened it.
R1 stands on the left and H1 on the right, with two port tags inside H1: port 2001 and port 2002. One link joins R1 and H1. Beside it, two dashed lines, the connections, run from just in front of R1 to the two tags, each with a dot at both ends. A note under them reads “one link, two connections”.
On R1, with both connections to H1 open:
Tap a line, or “Not in the output”, then submit.
Find the line of R1’s connection to the program on H1 port 2002.
R1#show tcp brief1Local Address Foreign Address (state)
Scroll sideways to see the whole output
R1 has two open connections with H1, one to each program, and it still waits on its own port 22.
- Column names: R1’s end (
Local Address), the far end (Foreign Address), the state. - Far end
10.0.1.10.2002, the program on H1 port 2002. R1’s end, 45525, is R1’s own number for this connection.ESTAB: open. - The second connection: R1’s end 32652 with port 2001. Both of its ports differ from the line above.
- R1 itself waits on port 22 (
LISTEN, no far end:*.*). A line is a connection to H1 only when its far end is H1’s address.
Full output · 5 lines
R1#show tcp briefTCB Local Address Foreign Address (state)7A585FB32948 10.0.1.1.45525 10.0.1.10.2002 ESTAB7A5866321DB0 10.0.1.1.32652 10.0.1.10.2001 ESTAB------------ 0.0.0.0.22 *.* LISTEN------------ ::.22 *.* LISTEN
Scroll sideways to see the whole output
A line with a port number is not always a connection. The two LISTEN lines carry port 22, but their far end is *.*, so they are R1 waiting for clients of its own, not connections to H1.
show sessions shows the same moment another way, with one entry for each connection R1 opened with telnet.
On R1, at the same moment:
Two connections that R1 started, both to 10.0.1.10, so R1 is the client of both.
R1#show sessionsConn Host Address1 1 10.0.1.10 10.0.1.101* 2 10.0.1.10 10.0.1.10
Scroll sideways to see the whole output
- The
*marks the current connection; here that is entry 2, the one opened last.
Full output · 3 lines
R1#show sessionsConn Host Address Byte Idle Conn Name 1 10.0.1.10 10.0.1.10 0 0 10.0.1.10* 2 10.0.1.10 10.0.1.10 0 0
Scroll sideways to see the whole output
show sessions gives no port, so it is show tcp brief, through the far end’s port, that tells which connection goes to which program.
Key idea
In R1’s list, each ESTAB line is one connection, R1’s end first and the far end second. R1 is the client here, so the far end’s port names the server program.
Troubleshoot
When a connection is refused or lost
An open connection has a program at each end. What does R1 show when there is no program at the far end?
Case 1 of 2.R1’s connection is refused
An arrow from R1 to a tag for H1 port 2003 is crossed out and marked “refused”.
Case 1 · Symptom
On R1, a client tries H1 port 2003 and gets no connection.
R1#telnet 10.0.1.10 2003Trying 10.0.1.10, 2003 ... 1% Connection refused by remote host
Scroll sideways to see the whole output
- H1 answered at once and refused the connection.
Case 1 · Command
show tcp brieflists R1’s other connections to H1. Are they still open?Case 1 · Output
The two open connections are untouched, and there is no line for port 2003.
R1#show tcp briefLocal Address Foreign Address (state)110.0.1.1.21519 10.0.1.10.2002 ESTAB110.0.1.1.54736 10.0.1.10.2001 ESTAB0.0.0.0.22 *.* LISTEN::.22 *.* LISTEN
Scroll sideways to see the whole output
- Key line Both connections, to ports 2002 and 2001, are still
ESTAB.
Full output · 5 lines
R1#show tcp briefTCB Local Address Foreign Address (state)71333BBE9F90 10.0.1.1.21519 10.0.1.10.2002 ESTAB71333BBE8AA0 10.0.1.1.54736 10.0.1.10.2001 ESTAB------------ 0.0.0.0.22 *.* LISTEN------------ ::.22 *.* LISTEN
Scroll sideways to see the whole output
- Key line Both connections, to ports 2002 and 2001, are still
Case 1 · What it means
- H1 is reachable: it answered at once, and its other connections stay open.
- No program waits on port 2003, so no connection forms.
- Fix it at port 2003: the link and the other programs are fine.
Case 1 · Fix
Start the program on H1 port 2003, or connect to the port where the program waits. Then, on R1:
R1#telnet 10.0.1.10 20031Trying 10.0.1.10, 2003 ... Open
Scroll sideways to see the whole output
Open: the connection to port 2003 is open.
Case 1 · Proof
R1’s list now has a third open connection.
R1#show tcp briefLocal Address Foreign Address (state)10.0.1.1.21519 10.0.1.10.2002 ESTAB10.0.1.1.54736 10.0.1.10.2001 ESTAB110.0.1.1.27690 10.0.1.10.2003 ESTAB0.0.0.0.22 *.* LISTEN::.22 *.* LISTEN
Scroll sideways to see the whole output
- The new line: far end
10.0.1.10.2003,ESTAB.
Full output · 6 lines
R1#show tcp briefTCB Local Address Foreign Address (state)71333BBE9F90 10.0.1.1.21519 10.0.1.10.2002 ESTAB71333BBE8AA0 10.0.1.1.54736 10.0.1.10.2001 ESTAB71333D332B98 10.0.1.1.27690 10.0.1.10.2003 ESTAB------------ 0.0.0.0.22 *.* LISTEN------------ ::.22 *.* LISTEN
Scroll sideways to see the whole output
- The new line: far end
End of case 1. Solved: R1’s connection to port 2003 is open.
Case 2 of 2.A program stops; R1 still lists two connections
R1 has two dashed lines to H1’s ports 2001 and 2002. The line to port 2001, where the program stopped, is drawn faint, with a cross on it, and its tag is faint too, with a dashed outline; the line to port 2002 is drawn in the normal color and marked open.
Case 2 · Symptom
The program on H1 port 2001 no longer receives data. Yet R1 still lists two connections.
R1#show sessionsConn Host Address1 1 10.0.1.10 10.0.1.101* 2 10.0.1.10 10.0.1.10
Scroll sideways to see the whole output
- This view shows no ports and no state, so it does not show which one is broken.
Full output · 3 lines
R1#show sessionsConn Host Address Byte Idle Conn Name 1 10.0.1.10 10.0.1.10 0 0 10.0.1.10* 2 10.0.1.10 10.0.1.10 0 0
Scroll sideways to see the whole output
Case 2 · Command
show tcp briefshows each connection’s state.Case 2 · Output
R1#show tcp briefLocal Address Foreign Address (state)110.0.1.1.65386 10.0.1.10.2001 CLOSEWAIT210.0.1.1.60541 10.0.1.10.2002 ESTAB0.0.0.0.22 *.* LISTEN::.22 *.* LISTEN
Scroll sideways to see the whole output
- Key line The connection to port 2001 shows
CLOSEWAIT, notESTAB. - The connection to port 2002 is still
ESTAB.
Full output · 5 lines
R1#show tcp briefTCB Local Address Foreign Address (state)7A78231DC858 10.0.1.1.65386 10.0.1.10.2001 CLOSEWAIT7A781C4ED578 10.0.1.1.60541 10.0.1.10.2002 ESTAB------------ 0.0.0.0.22 *.* LISTEN------------ ::.22 *.* LISTEN
Scroll sideways to see the whole output
- Key line The connection to port 2001 shows
Case 2 · What it means
- No program on H1 port 2001 takes data any more, and
CLOSEWAITshows that H1 has closed its end; R1’s end stays open until R1 closes it. - The connection to port 2002 is untouched: each connection is separate.
- No program on H1 port 2001 takes data any more, and
Case 2 · Fix
In
show sessions, the*marks the current connection; here that is entry 2, the one opened last. The connection to port 2001 was opened first, and it is entry 1. Close it withdisconnect 1, then check withshow tcp briefthat the 2001 line is gone.R1#disconnect 11Closing connection to 10.0.1.10 [confirm]
Scroll sideways to see the whole output
- R1 asks before it closes the connection to 10.0.1.10; press Enter to confirm.
Impact Closes this one connection on R1; the other one stays open.
Right after,
show tcp briefon R1 shows which line went away:R1#show tcp briefLocal Address Foreign Address (state)110.0.1.1.60541 10.0.1.10.2002 ESTAB0.0.0.0.22 *.* LISTEN::.22 *.* LISTEN
Scroll sideways to see the whole output
- Only the connection to port 2002 is left, with the same R1 end, 60541. The 2001 line is gone: entry 1 was the connection to port 2001.
Full output · 4 lines
R1#show tcp briefTCB Local Address Foreign Address (state)7A781C4ED578 10.0.1.1.60541 10.0.1.10.2002 ESTAB------------ 0.0.0.0.22 *.* LISTEN------------ ::.22 *.* LISTEN
Scroll sideways to see the whole output
When the program on H1 port 2001 runs again, R1 opens a new connection to it:
R1#telnet 10.0.1.10 20011Trying 10.0.1.10, 2001 ... Open
Scroll sideways to see the whole output
Open: a new connection to port 2001 is open.
Case 2 · Proof
Two open connections again, and no
CLOSEWAIT.R1#show tcp briefLocal Address Foreign Address (state)110.0.1.1.22275 10.0.1.10.2001 ESTAB210.0.1.1.60541 10.0.1.10.2002 ESTAB0.0.0.0.22 *.* LISTEN::.22 *.* LISTEN
Scroll sideways to see the whole output
- The new connection to port 2001:
ESTAB, with a new R1 end, 22275. - The connection to port 2002 is unchanged: same R1 end, 60541.
Full output · 5 lines
R1#show tcp briefTCB Local Address Foreign Address (state)7A781E13AB98 10.0.1.1.22275 10.0.1.10.2001 ESTAB7A781C4ED578 10.0.1.1.60541 10.0.1.10.2002 ESTAB------------ 0.0.0.0.22 *.* LISTEN------------ ::.22 *.* LISTEN
Scroll sideways to see the whole output
- The new connection to port 2001:
End of case 2. Solved: a new connection to port 2001 is open.
Both cases
Key idea
Refused: here, no program waits on that port. CLOSEWAIT: H1 has closed its end. Either way, the other connections keep working.
Check
On R1, after both connections were opened again:
Tap a line, or “Not in the output”, then submit.
Find the line of R1’s connection to port 22 on H1.
R1#show tcp briefLocal Address Foreign Address (state)
Scroll sideways to see the whole output
Full output · 5 lines
R1#show tcp briefTCB Local Address Foreign Address (state)7B94AE5C9CA8 10.0.1.1.39371 10.0.1.10.2001 ESTAB7B94B52BA610 10.0.1.1.20991 10.0.1.10.2002 ESTAB------------ 0.0.0.0.22 *.* LISTEN------------ ::.22 *.* LISTEN
Scroll sideways to see the whole output
Summary
R1 sits at the top left with its end, port 32652, beside it. A link runs straight down into H1, which holds two programs, on ports 2001 and 2002. A segment from 32652 to 2001 travels down beside the link inside a packet from 10.0.1.1 to 10.0.1.10, with an arrow beside the link pointing down; inside H1 the segment is handed to the program on 2001. The reply travels back up beside the link with both pairs swapped, to R1’s end 32652. The last step adds R1’s second end, 45525, and shows its two connections, 32652 with 2001 and 45525 with 2002, each a dashed line from R1’s end down to its program, beside the link.
- The address leads to an interface of the host; the port number finds the program on that host.
- A server waits on a known port number; a client starts the conversation from a number chosen for it.
- TCP opens a connection first and carries both port numbers in each segment; UDP carries both and opens no connection.
- On R1,
show tcp brieflists each connection as R1’s end, then the far end. - Here, refused means no program waits on that port; the other connections keep working.