当前位置:网站首页>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 )
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

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

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 .
边栏推荐
- How to brush leetcode
- Display logs in the database through loganalyzer
- 3. Hidden processes under the ring
- 阿里云开发板HaaS510报送设备属性
- PostgreSQL14安装使用教程
- SystemC common errors
- Tree reconstruction (pre order + middle order or post order + middle order)
- Des File Encryptor based on MFC framework
- SystemC:SC_ Thread and SC_ METHOD
- 【mysql进阶】mysql索引数据结构的演变(四)
猜你喜欢

Player actual combat 16 xdecode class

CSDN博客积分规则

单总线温度传感器18B20数据上云(阿里云)

通过loganalyzer展示数据库中的日志

Greed issues - Egypt scores

阿里云开发板HaaS510响应UART串口指令

Summary of virtual box usage problems

Redis核心配置和高级数据类型

What is automatic bidding? What are its advantages?

Briefly describe the difference between CGI and fastcgi
随机推荐
Wait function in SystemC
Démontage et modification de la machine publicitaire - décompression amateur
OAuth2学习中的一些高频问题的QA
Player practice 17 xvideowidget
TestEngine with ID ‘junit-vintage‘ failed to discover tests
Dismantle and modify the advertising machine - Amateur decompression
Player practice 11 audio resampling
Go language functions as parameters of functions
TestEngine with ID ‘junit-vintage‘ failed to discover tests
CUDA error: CUBLAS_ STATUS_ NOT_ INITIALIZED when calling `cublasCreate(handle)`
Player practice 15 xdemux and avcodecparameters
阿里云开发板vscode开发环境搭建
969. pancake sorting
Llvm 13.1 new pass plug-in form [for win]
Redis核心配置和高级数据类型
Display logs in the database through loganalyzer
Cmake basic tutorial - 01 a-hello-cmake
十四周作业
Alibaba cloud development board haas510 submission device attributes
动态搜索广告智能查找匹配关键字