# Tcp three handshake and four wave hands

TCP three-way handshake and four waved hands

OSI seven-layer model and TCP/IP four-layer model

Insert picture description here

Three handshake

Insert picture description here

TCP overview

Tcp regards the connection as the most basic object, and each connection has a source port and a destination port. Ip address + port gets a socket. E.g. 127.0.0.1:8080

TCP header format

Insert picture description here

  • Serial number: The sender informs the receiver that the data sent by the network packet is equivalent to the number of bytes of all sent data.

  • Confirmation response number: The receiver informs the sender that the receiver has received the first few bytes of all data.

  • Header length: indicates the starting position of the data part, and can also be considered to indicate the length of the header.

  • Reserved: This field is reserved and is not used now.

  • Control bits: URG : indicates that the emergency pointer field is valid; ACK : indicates that the received data sequence number field is valid, generally indicating that the data has been received by the receiver; PSH : indicates the data sent through the flush operation; RST : forced disconnection, used for abnormal interruption a case; the SYN : mutually transmitting and receiving the acknowledgment number, represents a linking operation; the FIN : disconnect represents

  • Window: The receiver informs the sender of the window size (that is, the amount of data that can be sent together without waiting for confirmation).

  • Checksum: used to check whether there is an error.

  • Urgent pointer: indicates the location of the data that should be processed urgently.

  • Optional fields: In addition to the fixed header fields above, optional fields can also be added.

TCP three-way handshake (3 packets are sent to each other when the connection is established)

purpose

Connect to the server to specify the port, establish a TCP connection, synchronize the serial number and confirmation number of the connected parties, and exchange TCP window size information.

process
  • The first handshake: When establishing a connection, the client sends a syn packet (syn=x) to the server, and enters the SYN_SENT state, waiting for the server to confirm; SYN: Synchronize Sequence Numbers.
  • The second handshake: the server receives the syn packet, must confirm the client's SYN (ack=x+1), and at the same time send a SYN packet (syn=y), that is, the SYN+ACK packet, and the server enters the SYN_RECV state;
  • The third handshake: the client receives the SYN + ACK packet from the server and sends an acknowledgment packet ACK (ack=y+1) to the server. After this packet is sent, the client and server enter the ESTABLISHED (TCP connection successful) state, which is completed three times shake hands.
    Insert picture description here

Wave four times

  • The client process sends a connection release message and stops sending data.

  • The server receives the connection release message and sends a confirmation message.

  • After the client receives the server's confirmation request, at this time, the client enters the FIN-WAIT-2 (terminated waiting 2) state, waiting for the server to send a connection release message (before that, it needs to accept the last data sent by the server) .

  • After the server sends the final data, it sends a connection release message to the client, and the server enters the LAST-ACK (last confirmation) state, waiting for the client's confirmation.

  • After the client receives the server's connection release message, it must send an acknowledgment. At this time, the TCP connection has not been released, and the longest message segment life must elapsed. When the client cancels the corresponding TCB, it enters the CLOSED state.

  • As long as the server receives the confirmation from the client, it immediately enters the CLOSED state. Similarly, after canceling the TCB, this TCP connection is ended. It can be seen that the server ends the TCP connection earlier than the client.
    Insert picture description here

Guess you like

Origin blog.csdn.net/qq_37248504/article/details/112547408