当前位置:网站首页>Tcp/ip network communication knowledge record

Tcp/ip network communication knowledge record

2022-06-12 14:12:00 Panda general

TCP/IP characteristic :

① Connection oriented transport layer protocol , A connection must be established before use , The established connection must be released after the transmission ;
② Every one of them TCP Connections can have only two endpoints , one-on-one ;
③TCP Provide reliable delivery of services , adopt TCP Continuously transmitted data , No mistakes 、 No loss 、 Do not repeat and arrive in order ;
④TCP Provide full duplex communication ,TCP Both ends are provided with sending buffer and receiving buffer , Used to temporarily store data for two-way communication ;
⑤ Byte stream oriented ;
Packet size limit 1460;
Socket Socket = (IP Address : Port number )
TCP/IP OSI A network model

Network knowledge

IP Address

It is divided into network number and host number ,IP Address (IPADDR) And mask (NETMASK) Perform bitwise and binary , Such as IP:192.168.6.63, Mask 255.255.255.0, The bitwise and subsequent network numbers are 192.168.6, Host number 63;

gateway

And IP The address is in the same network segment , All packets in the LAN are delivered to the gateway , The gateway is responsible for sending to the external network , Play a connecting role ;

Basic protocol number port number

Ethernet protocol number :IP:0x0800,ARP:0x0806;
IP Agreement No :ICMP:1,TCP:6,UDP:17;
Port number :FTP:20 21,SSH:22,Telnet:23,SMTP:25,HTTP:80;

TCP/IP Connection process ( Reprint )

Three handshake process

 Three handshake process
The first handshake : When establishing a connection , The client sends syn package (syn=x) To the server , And enter SYN_SENT state , Wait for server to confirm ;SYN: Sync sequence number (Synchronize Sequence Numbers).

The second handshake : Server received syn package , Must confirm customer's SYN(ack=x+1), At the same time, I also send a SYN package (syn=y), namely SYN+ACK package , At this time, the server enters SYN_RECV state ;

The third handshake : Client receives server's SYN+ACK package , Send confirmation package to server ACK(ack=y+1), This package has been sent , Client and server access ESTABLISHED(TCP Successful connection ) state , Complete three handshakes .

Four waves

 Four waves
1) The client process sends the connection release message , And stop sending data . Release data message header ,FIN=1, Its serial number is seq=u( It is equal to the sequence number of the last byte of the previously transmitted data plus 1), here , Client access FIN-WAIT-1( Stop waiting 1) state . TCP Regulations ,FIN Even if the message segment does not carry data , Also need to consume a serial number .
2) The server receives the connection release message , Send confirmation message ,ACK=1,ack=u+1, And bring your own serial number seq=v, here , The server enters CLOSE-WAIT( Turn off waiting ) state .TCP The server informs the high-level application process , The client is released in the direction of the server , It's half closed , That is, the client has no data to send , But if the server sends data , The client still has to accept . It's going to last a while , That is the whole CLOSE-WAIT The duration of the State .
3) After the client receives the confirmation request from the server , here , The client enters FIN-WAIT-2( Stop waiting 2) state , Wait for the server to send the connection release message ( Before that, you need to accept the last data sent by the server ).
4) After the server sends the last data , Send the connection release message to the client ,FIN=1,ack=u+1, Because it's half closed , The server probably sent some more data , Suppose the serial number at this time is seq=w, here , The server is in LAST-ACK( Final confirmation ) state , Wait for the client to confirm .
5) After the client receives the connection release message from the server , Confirmation must be sent ,ACK=1,ack=w+1, And my serial number is seq=u+1, here , The client enters TIME-WAIT( Time waits ) state . Note that this time TCP The connection has not been released , Must go through 2∗∗MSL( Maximum segment life ) After , When the client cancels the corresponding TCB after , Only enter CLOSED state .
6) As long as the server receives the confirmation from the client , Enter immediately CLOSED state . Again , revoke TCB after , It's the end of this TCP Connect . You can see , End of server TCP The connection time is earlier than the client .

Frequently asked questions

【 problem 1】 Why three handshakes when connecting , It's four handshakes when it's closed ?

answer : Because when Server End receipt Client Terminal SYN After connecting the request message , It can be sent directly SYN+ACK message . among ACK Messages are used to answer ,SYN Messages are used to synchronize . But when you close the connection , When Server End receipt FIN When the message , It's not likely to shut down immediately SOCKET, So you can only reply one first ACK message , tell Client End ,“ You sent it FIN I received the message ”. Only when I Server All messages are sent , I can send FIN message , So we can't send . So it takes four steps to shake hands .

【 problem 2】 Why? TIME_WAIT Status needs to pass 2MSL( Maximum segment lifetime ) To return to CLOSE state ?

reason 1: In order to ensure Client The last one sent ACK Messages can reach Server, This ACK Message segments may be lost , So in LAST-ACK State of Server Can't receive the sent FIN+ACK Confirmation of message segment ,Server It will be retransmitted over time FIN+ACK Message segment , and Client You can be in 2MSL Received this retransmission in time FIN+ACK Message segment , next Client Retransmission confirmation , Restart 2MSL timer , Last Client and Server All of them enter into CLOSED state . If Client stay TIME-WAIT State does not wait for a period of time , It's sending out ACK Release connection immediately after message segment , Then you can't receive Server Retransmission FIN+ACK Message segment , Therefore, the confirmation segment will not be sent again , such ,Server You can't follow the normal steps to enter CLOSED state .

reason 2: prevent “ Invalid connection request message segment ” Appears in this connection ,Client After sending the last ACK After message segment , After another time 2MSL, It can make all message segments generated during the duration of this connection disappear from the network , In this way, the old connection request segment will not appear in the next new connection .

【 problem 3】 Why not connect with two handshakes ?

answer : The main purpose is to prevent the invalid connection request message from being suddenly sent to the server , So there's a mistake ;
Possible situation : Invalid connection request message segment ” In such a case :client The first connection request segment sent is not lost , But in a network node for a long time , So that it will not arrive until some time after the connection is released server. Originally, this is a message segment that has already failed . but server After receiving this invalid connection request message segment , I mistook it for client A new connection request issued again . So I went to client Send confirmation message segment , Agree to establish a connection . Suppose you don't use “ Three handshakes ”, So as long as server Send a confirmation , A new connection is established . Because now client No connection request was made , So I won't pay any attention to server The confirmation of , Nor to server send data . but server But think that a new transport connection has been established , And kept waiting client Send data . such ,server A lot of resources are wasted . use “ Three handshakes ” The method can prevent the above phenomenon . For example, the situation just now ,client No direction server To send out a confirmation .server Because no confirmation can be received , You know client No connection required .

【 problem 4】 If a connection has been established , But what to do if the client suddenly fails ?

answer :TCP There is also a survival timer , obviously , If the client fails , The server can't wait , Waste resources in vain . The server will reset this timer every time it receives data from the client , The time is usually set to two hours , If you haven't received any data from the client in two hours , The server will send a detection segment , After every 75 Send once per second . If you send it in a row 10 Detection messages still don't respond , The server thinks the client is down , Then close the connection .

原网站

版权声明
本文为[Panda general]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/03/202203010513239225.html