4.17, TCP three-way handshake

1. TCP three-way handshake

  • TCPIt is a connection-oriented unicast protocol. Before sending data, both communicating parties must establish a connection between each other. The so-called "connection" is actually a piece of information about each other stored in the memory of the client and server, such as IPaddress , port number, etc.
  • TCPIt can be seen as a byte stream that handles packet loss, duplication, and errors at or below the IP layer. During the establishment of the connection, the two parties need to exchange some connection parameters. These parameters can be placed in TCPthe header .
  • TCPProvides a reliable, connection-oriented, byte stream, transport layer service that uses a three-way handshake to establish a connection. Takes four waves to close a connection.
  • The purpose of the three-way handshake is to ensure that the two parties have established a connection with each other.
  • The three-way handshake occurs when the client is connected. When called connect(), the bottom layer will TCPperform a three-way handshake through the protocol.
    insert image description here
  1. 16-bit port number (port number) : Inform the host where the segment comes from (source port) and which upper-layer protocol or application (destination port) it is transmitted to. TCPWhen communicating , the client usually uses an ephemeral port number automatically selected by the system.
  2. 32-bit sequence number (sequence number)TCP : TCPThe number of each byte of a byte stream in a certain transmission direction during a communication (from connection establishment to disconnection). Suppose 主机 Aand communicate with主机 B , in the first message segment sent to , the serial number value is initialized to a random value by the system . Then in this transmission direction , the sequence number value in the subsequent message segment will be set by the system to add the offset of the first byte of the data carried by the message segment in the entire byte stream. For example, if the data transmitted is the byte in the byte stream, then the sequence number value of the message segment is . The sequence number value of another transmission direction also has the same meaning.TCPABTCPISN(Initial Sequence Number,初始序号值)(从 A 到 B)TCPISNTCP 报文段1025 ~ 2048ISN + 1025(从B 到 A)TCP 报文段
  3. 32-bit acknowledgment number (acknowledgment number) : Used as a response to TCP 报文段a . Its value is the TCP 报文段received sequence number + flag bit length (SYN,FIN)+ data length . Assuming 主机 A to 主机 Bcommunicate TCPwith , then Athe sent by TCP 报文段not only carries its own serial number, but also Bcontains TCP 报文段the confirmation number of sent by . Conversely, Bthe sent TCPmessage segment also carries its own sequence number and Athe confirmation sequence number of the sent message segment.
  4. 4-bit header length (head length) : identifies the number of the TCPheader 32 bit(4 字节). Since 4 位the maximum can be expressed 15, so TCPthe head is the longest 60 字节.
  5. The 6 flag bits include the following items:
    • URG 标志, indicating whether the urgent pointer is valid.
    • ACK 标志, indicating whether the confirmation number is valid. We ACKcall TCPthe message segment carrying the flag a confirmation message segment.
    • PSH 标志, indicating that the receiving application program should immediately read the data from TCP the receiving buffer to make room for receiving subsequent data (if the application does not read the received data, they will stay TCPin the receiving buffer).
    • RST 标志, indicating that the other party is required to re-establish the connection. We RSTcall TCPthe segment carrying the flag the reset segment.
    • SYN 标志, indicating a request to establish a connection. We SYNcall TCPthe segment carrying the flag a synchronous segment.
    • FIN 标志, indicating that the other party is notified that the connection is to be closed. We FINcall TCPthe message segment carrying the flag the end message
      segment.
  6. 16-bit window size (window size) : is a means of TCPflow control. The window mentioned here refers to the Receiver Window (RWND) . It tells the other party how many bytes of data can be accommodated in the TCPreceiving , so that the other party can control the speed of sending data.
  7. 16-bit checksum (TCP checksum) : filled by the sender, and the TCPreceiver performs CRCan algorithm on the message segment to check TCPwhether the message segment is damaged during transmission. Note that this check includes not only TCPthe header , but also the data part. This is also an important guarantee for TCPreliable transmission.
  8. 16-bit urgent pointer (urgent pointer) : It is a positive offset. It is added to the value of the sequence number field to indicate the sequence number of the next byte of the last urgent data. Therefore, to be precise, this field is the offset of the urgent pointer relative to the current sequence number, which may be called the urgent offset. TCPThe urgent pointer is the method by which the sender sends urgent data to the receiver.

2. The specific process of TCP communication

第一次握手:
	1.客户端将SYN标志位置为1
	2.生成一个随机的32位的序号seq=J , 这个序号后边是可以携带数据(数据的大小)
第二次握手:
	1.服务器端接收客户端的连接: ACK=1
	2.服务器会回发一个确认序号: ack=客户端的序号 + 数据长度 + SYN/FIN(按一个字节算)
	3.服务器端会向客户端发起连接请求: SYN=1
	4.服务器会生成一个随机序号:seq = K
第三次握手:
	1.客户单应答服务器的连接请求:ACK=1
	2.客户端回复收到了服务器端的数据:ack=服务端的序号 + 数据长度 + SYN/FIN(按一个字节算)

① Three-way handshake

insert image description here
insert image description here

② Communication between server and client

insert image description here

Guess you like

Origin blog.csdn.net/z2812470857/article/details/130180508