xkonekt
Language: EN
  • English
  • Türkçe
  • Español, Coming soon
  • Português, Coming soon
  • Deutsch, Coming soon
  • Français, Coming soon

TCP and UDP · Introduction · Module 1

Ports: how one host keeps its conversations apart

Show topologyHide topology
R1routerH1host10.0.1.0/24Et0/0eth0.1.10Et0/0 = Ethernet0/0.1 = the last number of the address
Topology · One cable joins the router R1 and the host H1, on 10.0.1.0/24.

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.

R1to 10.0.1.10 · port 2001H1 · 10.0.1.10programport 2001programport 2002
Figure 1.1 · The address 10.0.1.10 brings data to H1; the port number decides which program on H1 receives it.

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.

Another program starts on H1 while the programs on ports 2001 and 2002 are still running. Can the new program wait on port 2001 too?

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.

client · startsserver · waitsR1H1startsreplyown number · 45525port 2002
Figure 1.2 · The client on R1 starts the conversation at the port where the server on H1 waits; the client’s end has its own number.

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.

A program on H1 starts a conversation with a program that waits on R1’s port 22. Which end is the server, and which port number does H1’s end use?

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

R1R1’s endport 32652H1 · 10.0.1.10programport 2001programport 2002

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

R1R1’s endport 32652segment · from 32652to 2001H1 · 10.0.1.10programport 2001programport 2002

TCP puts the data in a segment. Its header says from port 32652, to port 2001.

Step 3 of 7

R1R1’s endport 32652packet · from 10.0.1.1to 10.0.1.10segment · from 32652to 2001H1 · 10.0.1.10programport 2001programport 2002

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

R1R1’s endport 32652packet · from 10.0.1.1to 10.0.1.10H1 · 10.0.1.10segment · from 32652to 2001programport 2001programport 2002

The address 10.0.1.10 brings the packet to H1. H1 takes the segment out.

Step 5 of 7

R1R1’s endport 32652packet · from 10.0.1.1to 10.0.1.10H1 · 10.0.1.10segment · from 32652to 2001programport 2001programport 2002

H1 reads “to port 2001” and hands the data to that program. The program on 2002 receives nothing.

Step 6 of 7

R1R1’s endport 32652reply packet · from 10.0.1.10to 10.0.1.1segment · from 2001to 32652H1 · 10.0.1.10programport 2001programport 2002

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

R1R1’s endport 32652R1’s endport 45525H1 · 10.0.1.10programport 2001programport 2002connection32652–2001connection45525–2002

R1 has a second connection, from port 45525 to the program on port 2002. The two connections share both addresses, but their ports differ.

Step 1 of 7

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.

Figure 1.3 · Inside H1, the segment’s “to” port picks the program; the reply swaps the two numbers.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.TCP puts the data in a segment. Its header says from port 32652, to port 2001.The segment travels inside a packet. Its header says from 10.0.1.1, to 10.0.1.10. The packet crosses the link.The address 10.0.1.10 brings the packet to H1. H1 takes the segment out.H1 reads “to port 2001” and hands the data to that program. The program on 2002 receives nothing.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.R1 has a second connection, from port 45525 to the program on port 2002. The two connections share both addresses, but their ports differ.
At step 5 of Figure 1.3, H1 hands the data to the program on port 2001 and not to the one on 2002, though the packet named only H1’s address. Why?

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.

Output · IOS XE 17.18.2
1R1#telnet 10.0.1.10 20012Trying 10.0.1.10, 2001 ... Open
  1. The command: telnet, then H1’s address, then the port to connect to.
  2. 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.

Output · IOS XE 17.18.2 · some columns removed
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
  1. Column names: R1’s end (Local Address), the far end (Foreign Address), the state.
  2. R1’s end is address 10.0.1.1, then port 54329, a number chosen for this connection. ESTAB means the connection is open.
  3. 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

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.

The program on H1 port 2002 answers R1’s program on port 45525. Complete the reply segment: from port __ to port __.

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.

R1H1port 2001port 2002connectionone link, two connections
Figure 1.4 · Each connection between R1 and H1 has two ends: one on R1, one at a program’s port on H1.

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.

Output · IOS XE 17.18.2 · some columns removed
R1#show tcp brief1Local Address   Foreign Address  (state)

R1 has two open connections with H1, one to each program, and it still waits on its own port 22.

  1. Column names: R1’s end (Local Address), the far end (Foreign Address), the state.
  2. 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.
  3. The second connection: R1’s end 32652 with port 2001. Both of its ports differ from the line above.
  4. 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

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.

Output · IOS XE 17.18.2 · some columns removed
R1#show sessionsConn Host                Address1   1 10.0.1.10           10.0.1.101*  2 10.0.1.10           10.0.1.10
  1. 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 

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.

While R1’s two connections with H1 stay open, a program on R1 also sends short UDP messages to a program on H1. How many lines for R1’s conversations with H1 does show tcp brief list?

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

R1H1 · port 2003refused
Figure 1.5 · Case 1 · Where no program waits on the port, R1’s attempt is refused.

An arrow from R1 to a tag for H1 port 2003 is crossed out and marked “refused”.

  1. Case 1 · Symptom

    On R1, a client tries H1 port 2003 and gets no connection.

    Output · IOS XE 17.18.2
    R1#telnet 10.0.1.10 2003Trying 10.0.1.10, 2003 ... 1% Connection refused by remote host
    1. H1 answered at once and refused the connection.
  2. Case 1 · Command

    show tcp brief lists R1’s other connections to H1. Are they still open?

  3. Case 1 · Output

    The two open connections are untouched, and there is no line for port 2003.

    Output · IOS XE 17.18.2 · some columns removed
    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
    1. 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
  4. 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.
  5. Case 1 · Fix

    Start the program on H1 port 2003, or connect to the port where the program waits. Then, on R1:

    Output · IOS XE 17.18.2
    R1#telnet 10.0.1.10 20031Trying 10.0.1.10, 2003 ... Open
    1. Open: the connection to port 2003 is open.
  6. Case 1 · Proof

    R1’s list now has a third open connection.

    Output · IOS XE 17.18.2 · some columns removed
    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
    1. 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

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

R1H1 · port 2001program stoppedH1 · port 2002open
Figure 1.6 · Case 2 · When one program stops, R1’s other connection keeps working.

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.

  1. Case 2 · Symptom

    The program on H1 port 2001 no longer receives data. Yet R1 still lists two connections.

    Output · IOS XE 17.18.2 · some columns removed
    R1#show sessionsConn Host                Address1   1 10.0.1.10           10.0.1.101*  2 10.0.1.10           10.0.1.10
    1. 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 
  2. Case 2 · Command

    show tcp brief shows each connection’s state.

  3. Case 2 · Output

    Output · IOS XE 17.18.2 · some columns removed
    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
    1. Key line The connection to port 2001 shows CLOSEWAIT, not ESTAB.
    2. 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
  4. Case 2 · What it means

    • No program on H1 port 2001 takes data any more, and CLOSEWAIT shows 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.
  5. 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 with disconnect 1, then check with show tcp brief that the 2001 line is gone.

    Output · IOS XE 17.18.2
    R1#disconnect 11Closing connection to 10.0.1.10 [confirm]
    1. 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 brief on R1 shows which line went away:

    Output · IOS XE 17.18.2 · some columns removed
    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
    1. 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

    When the program on H1 port 2001 runs again, R1 opens a new connection to it:

    Output · IOS XE 17.18.2
    R1#telnet 10.0.1.10 20011Trying 10.0.1.10, 2001 ... Open
    1. Open: a new connection to port 2001 is open.
  6. Case 2 · Proof

    Two open connections again, and no CLOSEWAIT.

    Output · IOS XE 17.18.2 · some columns removed
    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
    1. The new connection to port 2001: ESTAB, with a new R1 end, 22275.
    2. 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

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.

R1 holds connections to H1 ports 2001 and 2002. The program on port 2002 stops. What does R1’s show tcp brief show for the connection to port 2001?

Check

A program on R1 sends data to 10.0.1.10 with port number 2002. What does each value decide?
A program on R1 sends one short message to a program on H1. It has no need to open a connection first. Which set of rules does it use, TCP or UDP, and what does the message carry?
R1 holds connections to the programs on H1 ports 2001 and 2002. The program on port 2002 stops. A moment later a program on R1 tries to open a new connection to H1 port 2002. What happens?

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.

Output · IOS XE 17.18.2 · some columns removed
R1#show tcp briefLocal Address  Foreign Address (state)
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
R1 holds two connections with H1: from R1’s port 39371 to H1’s port 2001, and from R1’s port 20991 to H1’s port 2002. The program on port 2002 sends data back to R1. Which port numbers does that segment carry?

Summary

R1R1’s endport 32652R1’s endport 45525H1 · 10.0.1.10programport 2001programport 2002connection32652–2001connection45525–2002
Figure 1.3, recap · 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.

  • 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 brief lists each connection as R1’s end, then the far end.
  • Here, refused means no program waits on that port; the other connections keep working.