seq: Initial serial number

Three handshakes
for the first time : When establishing a connection , Client send SYN package ( Synchronization sequence number )SYN=j To server , And enter SYN_SENT state , Wait for server confirmation

The second time : Server received SYN package , The of the client must be confirmed SYN(ack=j+1), Send one yourself at the same time SYN package (syn=k), Namely SYN+ACK package , The server enters STN_RECV state

third time : The client receives a message from the server SYN+ACK package , Send confirmation package to the server ACK(ack=x+1), This packet has been sent , Client and server access EXTABLISHED state , Connection successful

Three handshakes is to establish a TCP When connecting , The client and server need to send a total of three packets , The main purpose of three handshakes is to confirm whether the receiving and sending abilities of both parties are normal , Specify your own initialization serial number to prepare for subsequent reliability transmission
The essence is to connect to the specified port of the server TCP connect , And synchronously connect the serial numbers and confirmation numbers of both parties , exchange TCP Window size information

Why three handshakes ?
1. The client sends the network packet and the server receives it ( Client sending capability , The acceptance capacity of the server side is normal )
2. Service end contracting , Client received ( The client understands the sending of the server , Normal acceptance , Client acceptance , Normal transmission capability )
3. Received by client and server ( client , The acceptance capacity of the server is normal )

Four waves
for the first time : host 1 set up seq ack, To host 2 Send a FIN Message segment , host 1 get into FIN_WAIT_1 state , Indicate host 1 No data has been sent to the host 2
The second time : host 2 Received from host 1 of FIN Message segment , To host 1 Reply to one ACK Message segment (ack+1
seq+1), host 1 get into FIN_WAIT_2 state , Indicate host 2 Agree host 1 Close request for
third time : host 2 To host 1 send out FIN Message segment , Request to close connection , host 2 get into LAST_ACK state
Fourth time : host 1 Received host 2 Sent FIN Message segment , To host 2 send out ACK Message segment , host 1 get into TIME_WAIT state , host 2 Received host 1 of ACK The message segment closes the connection
host 1 Need to wait 2MSL( Maximum message lifetime ) No reply was received after the , Proof host 2 It has been closed normally , Then host 1 Can close

FIN_WAIT_1 state :SOCKET stay ESTABLISHED state , Actively close the connection to the , Sent to the other party FIN Message segment , Enter at this time FIN_WAIT_1
And when the other party responds ACK after , get into FIN_WAIT_2

Why four waves ?
TCP Full duplex mode
host 1 send out FIN Message segment : host 1 No data has been sent to the host 2, Tell the host 2: My data has been sent , But you can still accept from the host 2 Data
host 2 send out ACK Message segment : Understand the host 1 There is no data to send , But the host 2 Data can still be sent to the host 1
host 2 send out FIN Message segment : Indicate host 2 No data to send
host 1 send out ACK Message segment : Close connection

Why do clients wait 2MSL?

1. Ensure the last one sent by the client ACK Message arrival server . if ACK Message segment missing , Server thinks : I've sent it FIN+ACK Request disconnected , The client is not responding , I'll send it again . And the client is 2MSL The retransmission message from the server is received within the time limit , Then respond and start 2MSL timer
2. Client sends last ACK, stay 2MSL In time , All message segments generated during the time of this connection can disappear from the network , The request message of the old connection will not appear in the new connection