TCP Connections and Port Numbers
Two host applications using TCP must establish a TCP connection before data can flow. Each connection exists between a pair of TCP sockets, with a socket being defined as an IP address of the host, the port number used, and the transport layer protocol (TCP in this case). The connection establishment process essentially initializes the sockets, including the source and destination ports, as well as the Sequence and Acknowledgement fields. Figure 6-2 depicts a typical three-way TCP connection establishment, as well as the typical four-way connection termination process.
Figure 6-2 TCP Connection Establishment and Termination
TCP Connection Establishment, Initiated by Client
SPORT=1027, DP0RT=80, SEQ=200, SYN
SP0RT=80, DP0RT=1027, SEQ=1450, ACK=201, SYN, ACK
Web Server ^_sport=1027, dport=80, seq=201, ack=1451, ack Web Browser
TCP Connection Termination, Initiated by Client
SP0RT=1027, DP0RT=80, ACK, FIN
SP0RT=1027, DP0RT=80, ACK
In the connection establishment phase, the two hosts select port numbers, select the Sequence and Acknowledgement fields, and use TCP code bits to identify the messages in the three-way handshake for connection establishment. First, for port numbers, the server must already be listening for connection requests from clients, with those requests being to a particular well-known port—in this case, HTTP port 80. (Well-known ports are listed at http://www.iana.org.) The client picks a currently unused port number to use as the source port, typically a value of 1024 or greater. Note in Figure 6-2 that when comparing the segments going in opposite directions, the Port Number fields are reversed.
The TCP header includes several 1-bit fields, called code bits orflags, that are used for a variety of purposes. The SYN and ACK flags identify segments as either the first or second in a new TCP connection: a segment with just the SYN flag set is the first segment in a new connection, and a segment with both SYN and ACK set is the second segment in a new connection. These flags allow hosts to easily recognize new connection requests.
The initial sequence numbers can be set to any valid value, and often are not set to 0. Remember that error recovery, using these fields, happens independently in both directions. So, the first segment in the three-way handshake of Figure 6-2 sets the left-to-right sequence number; the second segment sets the right-to-left sequence number and also acknowledges the first segment; and the third segment acknowledges the right-to-left sequence number.
Connection termination can be accomplished in one of several ways. The most benign case uses a four-segment flow, as shown in the bottom half of Figure 6-2, with the ACK and FIN flags being used.
Continue reading here: TCP Header Miscellany
Was this article helpful?
Readers' Questions
-
Layton17 days ago
- Reply
-
aurelia2 months ago
- Reply