How TCP works: 4. Three-way handshake)

Source: Internet
Author: User

The preceding three sections introduce the implementation of the socket mechanism for TCP three-way handshake. It should be emphasized that, similar to the Protocol-independent implementation, TCP three-way handshake is independent from the socket system theory. In TCP, the three-way handshake is implemented through three IP datagram formats. The IP datagram in TCP format contains the TCP Header, And the TCP header information contains the description of the specific content of each datagram. The first bit mark we need to introduce here is only three:
SYN: the synchronization sequence number is used to initiate a connection. Because the TCP protocol requires data transmission to be reliable, its implementation method is to number each byte of the transmitted data in sequence. However, the initial serial number (ISN: Initial Sequence Number) does not start from 0, but is a 32-bit unsigned integer that changes repeatedly over time. When one party initiates a connection, SYN is set to 1. At the same time, a byte is used to indicate that this is the beginning of a new connection. Therefore, assume that the ISN of the initiator is N, because SYN will add a byte in the data section to indicate that this is the beginning of a new connection, so the byte number is n + 1 at this time.
Ack: confirm that the serial number is valid. TCP requires that data reliability be verified automatically. The implementation method is to check whether the byte serial number is correctly connected. If the serial number of the recipient is m, the serial number returned to the sender is m + 1. Once connected, Ack is always set to 1, indicating that the serial number is valid and always exists in all packets. However, whether the data is actually used by TCP depends on whether the serial number can be matched. If the byte serial number sent by the sender does not start from m + 1, this IP packet will not be used, and the returned ack information serial number is still m + 1; if the byte serial number sent from the sender starts from m + 1, but an error occurs during verification, the datagram is still not used, and the returned ack information serial number is still m + 1. The serial number will not be added until the data passed the TCP check is received. For example, the byte serial number of incoming data starts from m + 1 to m + k and ends with TCP verification, then the ACK information returned again becomes m + k + 1.
Fin: the sender completes sending. Like SYN, Fin also occupies one byte in the data part, indicating that it is an ending symbol.
The three-way handshake process of TCP is as follows:
1. The first SYN connection request is initiated by the client. This datagram sets SYN to 1, indicating that it is a connection request and contains the ISN of the connection. We assume the value is N.
2. The server starts to build the feedback datagram after receiving the datagram from the first handshake request. The feedback datagram includes two parts: the first part is to feedback the serial number of the Connection Request back, because SYN occupies a byte, so the serial number returned is n + 1; the second part is to initiate a SYN connection request to the client, set SYN to 1, and include the ISN of the new connection. We set the value to M.
3. The client responds to the server-side SYN connection request and returns the server-to-client connection serial number. Because SYN occupies one byte, the serial number sent to the server is m + 1.
From this, we can see that in TCP, the client-to-server and the server-to-client connections are established separately, with different isN (N and m ), as we can see later, this means that the two connections need to be disconnected separately under normal circumstances.

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.