当前位置:网站首页>API related to TCP connection
API related to TCP connection
2022-07-05 05:35:00 【Raise items】
List of articles
On establishment and termination
TCP
When connecting , Both ends of the communication will send some messages with Special mark ( Such as SYN
、 ACK
、 FIN
、 RST
) Message of , and Maintain their respective States . You can put With special marks And Without data information Of tcp Message as
Protocol message
; hold Carry data information Of tcp Message asThe data packet
.
Establishing a connection
Three handshake process :
TCP The process of establishing a connection is transparent to the user process .
In the process , Client blocked on connect
system call , The server is blocked in accept
system call .
// #include <sys/types.h>
// #include <sys/socket.h>
int connect(int socket, const struct sockaddr *address, socklen_t address_len);
int accept(int socket, struct sockaddr *restrict address, socklen_t *restrict address_len);
If TCP
Connection established successfully ,connect
return 0
,accept
return Connect socket The descriptor ; If the connection fails , All back to -1
.
Only after the connection is established 、 Until the termination of , Both parties can send The data packet . in fact , The third handshake , The client can Carrying data Send it to the server .
Terminate connection
The process of terminating the connection is more complicated , Involved API Also more . The initiator of terminating the connection may be either the server or the client , Take client initiation as an example , Four waves :
2MSL
Is the longest segmented life span MSL
(maximum segment lifetime) Twice as many , This design is to ensure that in When the connection terminates , There is no message related to this connection in the network .
After the first two steps , Get into Half closed state : The client cannot send data to the server , But the server can send data to the client . When the client and the server All in CLOSED state after ,TCP The connection is officially terminated .
have access to close
or shutdown
Terminate connection , The usage of the two is slightly different .
close
// #include <unistd.h>
int close(int fildes);
close
Can do two things :
- Send a... To the opposite end
FIN
package . here , The opposite user process will readEOF
, namelyread
The function returns0
. If analogy becomes a read file , This means that you have read the file At the end of . - Close to socket Of Read and write . Although at this time in
FIN_WAIT_2
state , But if there is a data message sent from the opposite end , The local end cannot read , And will return to the other party aRST
message , namely To the end socket Will be reset, And in theRST
Of socket Write operation , It will triggerSIGPIPE
The signal ( The default behavior is Process exits directly ). Cannot pass the current socket send data .
shutdown
// #include <sys/socket.h>
int shutdown(int socket, int how);
shutdown
The function will choose to close the pair socket read 、 Write perhaps Reading and writing .
- Turn off reading : Do not send
FIN
message ; If there is data sent from the opposite end , No reception , Return one to the otherRST
message . - Turn off write : send out
FIN
message , Cannot pass this socket send data . - Turn off reading and writing : Be similar to
close
.
If a socket Of Reference count Greater than
1
,close
It won't close , andshutdown
The corresponding socket.
RST message
When you encounter some abnormal Scene time , The client or server will send a RST
message . These scenes include :
- Customer requests connection , The relevant services are not enabled 、 When it is not present or fails , The customer will receive a
RST
message . - towards Read turned off Of socket When sending data , I'll get one
RST
message .
边栏推荐
- How many checks does kubedm series-01-preflight have
- Yolov5 ajouter un mécanisme d'attention
- [to be continued] [depth first search] 547 Number of provinces
- object serialization
- 每日一题-无重复字符的最长子串
- 搭建完数据库和网站后.打开app测试时候显示服务器正在维护.
- [to be continued] [UE4 notes] L3 import resources and project migration
- Haut OJ 1241: League activities of class XXX
- To be continued] [UE4 notes] L4 object editing
- 动漫评分数据分析与可视化 与 IT行业招聘数据分析与可视化
猜你喜欢
Palindrome (csp-s-2021-palin) solution
Pointnet++学习
SAP method of modifying system table data
sync. Interpretation of mutex source code
YOLOv5添加注意力機制
【Jailhouse 文章】Jailhouse Hypervisor
Personal developed penetration testing tool Satania v1.2 update
Gbase database helps the development of digital finance in the Bay Area
YOLOv5添加注意力机制
Educational Codeforces Round 116 (Rated for Div. 2) E. Arena
随机推荐
Zzulioj 1673: b: clever characters???
Personal developed penetration testing tool Satania v1.2 update
使用Electron开发桌面应用
【ES实战】ES上的native realm安全方式使用
CF1637E Best Pair
Sword finger offer 06 Print linked list from beginning to end
A preliminary study of sdei - see the essence through transactions
Reflection summary of Haut OJ freshmen on Wednesday
【Jailhouse 文章】Jailhouse Hypervisor
读者写者模型
全国中职网络安全B模块之国赛题远程代码执行渗透测试 //PHPstudy的后门漏洞分析
Using HashMap to realize simple cache
全排列的代码 (递归写法)
[binary search] 34 Find the first and last positions of elements in a sorted array
剑指 Offer 06.从头到尾打印链表
浅谈JVM(面试常考)
Demonstration of using Solon auth authentication framework (simpler authentication framework)
记录QT内存泄漏的一种问题和解决方案
MySQL数据库(一)
[jailhouse article] performance measurements for hypervisors on embedded ARM processors