Establishing a TCP connection: three-way handshake

Publisher:丹青妙手Latest update time:2015-06-12 Source: 51hei Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
TCP is connection-oriented. In a connection-oriented environment, a connection must be established between two terminals before data transmission begins. The process of establishing a connection ensures that both parties are ready to transmit and receive data before sending application data packets.
The two communicating parties must synchronize with each other using the initialization sequence number seq and the acknowledgment number ack from the other party to confirm the successful transmission. (The ack number is the number of the next octet that is expected to be received.) The synchronization signal is usually written as SYN and the acknowledgment signal is ACK. The entire synchronization process is called a three-way handshake.
 
 
1) Host A sends SYN to host B: My sequence number seq is X.
2) Host B sends SYN and ACK to host A: My sequence number seq is X+1, and the response number is X+1 (waiting to receive the X+1th octet).
3) Host B sends SYN and ACK to Host B: My sequence number seq is X+1, and the response number is Y+1.
Through the above three steps (three-way handshake), the TCP connection is established and data transmission begins. TCP on any machine can passively wait for handshakes or actively initiate handshakes. Once the connection is established, data can flow in both directions in a peer-to-peer manner.

 
If TCP uses 1 as the initialization sequence number for each connection, when the local system is restarted, the remote system will think that the previous connection still exists. So each time a connection is made, the host will randomly select an initialization sequence number and use it to identify the position of the transmitted octet in the data stream. Then both parties have to negotiate their respective sequence numbers, because when the receiver receives the first SYN, he does not know whether it is a delayed old signal. So it must request to send this SYN to verify.

 
In general, TCP uses the least information segment to implement the three-way handshake, which is effective in reducing network communication traffic. In short, the three-way handshake makes both parties ready to transmit data and unifies the initialization sequence number.

 
Closing a TCP connection: Improved three-way handshake
For an established connection, TCP uses a modified three-way handshake to end the call (using a segment with a FIN additional flag).
1) When the application on host A notifies TCP that the data has been completed, TCP sends a segment with a FIN additional mark to host B (FIN is understood as
finish).
2) After receiving the FIN segment, host B does not immediately reply to host A with a FIN segment, but instead sends an ACK to host A.
At the same time, notify the corresponding application: the other party requests to close the connection (send ACK first to prevent the other party from retransmitting the FIN segment during this period of time).
3) The application on host B tells TCP: I want to completely close the connection, and TCP sends the second FIN segment to host A.
4) After receiving the second FIN segment, host A sends an ACK to host B to indicate that the connection is completely closed.

 
TCP Reliability
TCP is stream-oriented, that is, data segments are transmitted as a sequence of bytes.
When the connection is established through the three-way handshake, the sequence number is initialized. During the transmission process, TCP continues to use this sequence number to mark each data segment sent. Every time a data segment is sent, the sequence number is increased by 1. The receiving station reassembles the received data segment based on the sequence number. Why do we rely on sequence numbers to reassemble data segments?
For example, on a network with both high-speed links and low-speed links, the data segments on the high-speed link may arrive earlier than those on the low-speed link. In this case, the sequence number must be relied upon to reassemble the data segments. This is one of the functions of the sequence number.

 
During the transmission process, the role of the confirmation number ACK is to tell the sender which data packets have been successfully received, and the confirmation number will indicate to the sender the sequence number of the next data segment that the receiver expects to receive. This mechanism is called expected confirmation, that is, the confirmation number is equal to the next expected byte.

 
In TCP/IP networks, there are two phenomena: timeout and retransmission. If a data segment with a certain sequence number is lost during transmission, resulting in the sender not receiving a response for that data segment within a given time interval, the lost data segment will be required to be retransmitted. The data segment will be stored in the sender's buffer until the sender receives a response number, at which point it will release the buffer. This mechanism is called Positive Acknowledgement and Retransmission (PAR), which is a technology used by the communication protocol to ensure credibility.

 
The second function of the sequence number is to eliminate duplicate packets in the network (synchronous replication). For example, when the network is congested, if the sender has not received the ACK packet of a data segment from the receiver, it may think that the data segment with this sequence number is lost, so it will resend it. This may cause the receiver to receive two data segments with the same sequence number after the network returns to normal. At this time, the receiver will automatically discard the second identical data segment.

 
The sequence number and acknowledgment number provide an error correction mechanism for TCP, thus improving the reliability of TCP.
Reference address:Establishing a TCP connection: three-way handshake

Previous article:About arrays as function parameters
Next article:Research on Motor Temperature Monitoring System Based on TMP275

Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号