TCP transmission connection establishment and release details

Source: Internet
Author: User

TCP transmission connection establishment and release details
TCP transmission connection is a connection-oriented transport layer protocol. Therefore, a transmission connection must be established between the two parties before any data is sent to the other party. This section describes in detail how a TCP transmission connection is established. 1. during the establishment of a TCP connection with a single active connection, the TCP protocol in the TCP/IP architecture also uses the three-way handshake mechanism to establish a transmission connection, this is the same as the three-way handshake mechanism adopted by the OSI/RM transport layer to avoid repeated connections described earlier in this chapter. As shown in the specific process 10-38, the overall process is fully reflected in figure 10-37 in the previous section. Here, only the TCP transmission connection establishment process is listed separately. The specific steps are as follows: (1) The first step is the server initialization process. Starting from the CLOSED (CLOSED) status, the SOCKET, BIND, LISTEN, and ACCEPT primitives are called sequentially to create Socket sockets, enter the LISTEN status and wait for the TCP transmission connection request from the client. (2) The client also calls the SOCKET primitive from the CLOSED state to create a new Socket SOCKET, and then calls the CONNECT primitive as needed, send a data segment that sets the SYN field to 1 (indicating the synchronous data segment) to the server (assuming the initial number is I), open the port, and enter the SYN SENT (the connection request has been SENT, wait for confirmation) status. Figure 10-38 three-way handshake established by the TCP transmission connection (3) After the server receives the SYN data segment from the client, it sends back a SYN field and sets it to 1 (indicating that this is the synchronous data segment ), if the ACK field is set to 1 (this is the validation data segment), ack (confirmation number) = the response data segment of I + 1 (assuming the initial serial number is j), and the port is opened passively, enter the syn rcvd status (a connection request has been received but not confirmed. Note that the validation number is I + 1 rather than I, indicating that the next data segment number the server wants to receive is I + 1. (4) After receiving the SYN + ACK data segment from the server, the client sends an ACK = 1 (indicating that this is the validation data segment) to the server. The serial number is I + 1, ack = j + 1 confirms the data segment and enters the ESTABLISHED (connection ESTABLISHED) status to establish a one-way connection. Note that the serial number is I + 1 and the validation number is j + 1, indicating that the client wants to receive the serial number j + 1 of the next data segment of the server. (5) After receiving the ACK data segment from the client, the server enters the ESTABLISHED status to establish a bidirectional connection. The connection can be initiated by either party or both parties. Once the connection is established, data can flow in two directions, without the so-called master-slave relationship. The three-way handshake is a sufficient condition for correct synchronization between the two ends of the connection. Because TCP is established on an unreliable group transmission service, packets may be lost, delayed, duplicated, and out-of-order, therefore, the Protocol must use the timeout and retransmission mechanisms. If the retransmission connection request and the original connection request arrive when the connection is being established, or a delayed connection request arrives after a connection has been established, used, and ended, the problem may occur. The three-way handshake protocol can solve these problems. For example, the ACK Data Segment sent by the client is used to avoid repeated connections due to network latency, because the client can check the confirmation number in the ACK data segment to check whether the connection request has expired. [Experience] Figure 10-38 is compared with figure 10-20 in section 10.2.2 above this chapter, in general, the establishment of TCP transmission connections is similar to the establishment of TP transmission connections in OSI/RM (for example, the three-way handshake mechanism), and there is a large area. Mainly manifested in: ① in OSI/RM, only dt tpdu and ed tpdu have serial numbers, so in the returned validation class TPDU, there is no TPDU serial number, therefore, the serial number of the TPDU is not marked in Figure 10-20. In the TCP transmission connection, each data segment (whether or not it carries data) has a serial number, and the corresponding serial number must be marked; ② different service primitives can use different types of TPDU in OSI/RM transmission protocols, therefore, the TPDU of OSI/RM does not have control spaces such as ACK and SYN in TCP data segments, because the formats of all data segments in TCP are the same, the difference is that these fields of different types of TCP data segments have different values; ③ The use of the "validation" field in the TCP transmission connection is exactly the same as that in the "YR-TU-NR" (your TPDU serial number) field in the OSI/rm tpdu, the serial number of the next data segment to be received by the peer, indicating that all data segments before the number have been correctly received. 2. when both parties establish a TCP connection at the same time, normally, the transmission connection is initiated by one party, but it is also possible that both Parties initiate a connection at the same time, and a connection collision will occur at this time, in the end, only one connection can be established. Because all connections are identified by their endpoints. If the first connection request establishes a connection identified by socket (x, y), and the second connection also establishes such a connection, there is only one socket table item in the TCP object. When a connection request is sent at the same time, the two ends send a data segment with the SYN field set to 1 almost simultaneously and enter the SYN_SENT state. When each end receives the SYN data segment, the status changes to SYN_RCVD. At the same time, both ends send the data segment where the SYN field is set to 1 and the ACK field is set to 1 to confirm the received SYN data segment. When both parties receive the SYN + ACK data segment from the other party, the request enters the ESTABLISHED status. Figure 10-39 shows the connection process for initiating a connection at the same time, but a TCP connection is established instead of two. Pay special attention to this. Figure 10-39 shows the process of establishing a TCP connection that initiates a connection at the same time. A transmission connection opened by both parties needs to exchange 4 data segments, exchange one data segment more than the three-way handshake established by the normal transmission connection. In addition, we do not call any end a customer or server because each end is both a customer and a server. 10.3.7 after the release TCP connection of the TCP transmission connection is established, data streams can be transmitted in two directions. When no data needs to be sent by the TCP network application process, you can issue the disconnect command to release the connection. The TCP protocol uses sending the data segment with the FIN field set to 1 as the command to disable the transmission connection. However, the local end can continue to receive data from the peer end, until the peer end uses the same method to close the data stream in that direction, the transmission connection between the two sides is completely closed. 1. The release process of a TCP connection that is manually closed is a little more complex than the three-way handshake process established by a TCP transmission connection. Four handshakes are required. This is caused by the half-close feature of TCP, that is, because this TCP connection is full duplex (that is, data can be transmitted simultaneously in both directions ), each direction must be closed separately. When one party completes its data transmission task, it can send a data segment with the FIN field set to 1 to terminate data transmission in this direction; when the other end receives the FIN data segment, it must notify its application layer that "the peer end has terminated the data transfer in that direction ". The sending of the FIN data segment is the result of the application layer calling the CLOSE service primitive. The process of the four handshakes released by the TCP connection is 10-40. The specific description is as follows: (1) at the beginning, both parties are in the ESTABLISHED status. If the client considers that all data has been sent and wants to end the connection, the application process at the application layer calls the CLOSE service primitive, then, a data segment with the FIN field set to 1 is sent to the server (assuming the serial number of this data segment is m). The client enters the fin wait 1 status and waits for confirmation from the server. (2) After receiving the FIN data segment from the client, the server confirms that no new data is sent from the client and sends an ACK field to the client to set it to 1, the confirmation number is m + 1 (assume that the data segment number is w, and the data segment number of the server and the client can be different), indicating that all the preceding data has been received, then enter the close wait (close wait) status. At the same time, the TCP entity of the server notifies the corresponding application-layer process, releases the transmission connection from the client to the server, and enters the semi-closed state. However, the server can still send data segments to the client. The client can also receive data from the server. This may take some time until all the server data is sent. Figure 10-40 four handshakes released by the TCP transmission connection (3) the client enters the fin wait 2 status after receiving the ACK data segment of the server, the data segment to be released after the server sends a connection. (4) when the server sends full data, the corresponding application process will also notify the TCP entity to release the TCP transmission connection in this direction, and send the FIN field to the client to set 1, set the ACK field to 1, and ack = m + 1 (assuming that the data segment number has changed to w. The server enters the last ack status and waits for confirmation from the client. (5) After receiving the server's FIN + ACK data segment, the client sends an ACK field to the server to set 1, ack = w + 1, and the serial number is m + 1, enter the time wait Status. However, the TCP connection has not been released yet. The client enters the CLOSED State only after 2MSL time (RFC 793 recommends setting MSL to 2 minutes), and the TCP connection is completely released. (6) After receiving the ACK data segment from the client, the server enters the CLOSED status and completely releases the connection. The entire TCP transmission connection release process is completed. 2. the release process of the TCP connection that both parties close is the same as setting up a TCP transmission connection at the same time, the TCP transmission connection can also be closed at the same time by both parties (normally, one Party sends the First FIN data segment for the active connection to close, and the other party passively accepts the connection to close), as shown in Figure 10-41. Figure 10-41 simultaneously closes TCP connections. When the network application layer processes at both ends call the CLOSE primitive at the same time and send the FIN data segment to execute the CLOSE command, both ends are changed from the ESTABLISHED status to the fin wait 1 status. After receiving the FIN data segment from the peer end, either party changes its status from fin wait 1 to CLOSING and sends the last ACK data segment. When the last ACK data segment is received, the status changes to TIME_WAIT. After waiting for 2MSL, it enters the CLOSED status and finally releases the entire TCP transmission connection.
 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.