news 2026/2/3 6:31:57

解密Linux TCP网络协议栈的工作原理

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
解密Linux TCP网络协议栈的工作原理

、TCP服务器调用的API

代码语言:javascript

AI代码解释

#include <sys/types.h> /* See NOTES */ #include <sys/socket.h> // 1 int socket(int domain, int type, int protocol); // 2 int bind(int sockfd, const struct sockaddr *addr, socklen_t addrlen); // 3 int listen(int sockfd, int backlog); // 4 ssize_t recv(int sockfd, void *buf, size_t len, int flags); // 5 int accept(int sockfd, struct sockaddr *addr, socklen_t *addrlen); // 6 ssize_t send(int sockfd, const void *buf, size_t len, int flags); // 7 int close(int fd); // 8 int shutdown(int sockfd, int how);

1.2、TCP客户端调用的API

代码语言:javascript

AI代码解释

#include <sys/types.h> /* See NOTES */ #include <sys/socket.h> // 1 int socket(int domain, int type, int protocol); // 2 int bind(int sockfd, const struct sockaddr *addr, socklen_t addrlen); // 3 int connect(int sockfd, const struct sockaddr *addr, socklen_t addrlen); // 4 ssize_t send(int sockfd, const void *buf, size_t len, int flags); // 5 ssize_t recv(int sockfd, void *buf, size_t len, int flags); // 6 int close(int fd); // 7 int shutdown(int sockfd, int how);

1.3、API函数的作用

(1)int socket(int domain, int type, int protocol) 在文件系统中分配一个fd,并创建TCB数据结构。

(2)int bind(int sockfd, const struct sockaddr *addr, socklen_t addrlen) 为TCP的socket绑定本地IP地址和端口。

(3)int listen(int sockfd, int backlog) 将TCP置于LISTEN状态。

(4)int accept(int sockfd, struct sockaddr *addr, socklen_t *addrlen) 从全连接队列中取出一个节点,并分配一个fd。

(5)ssize_t recv(int sockfd, void *buf, size_t len, int flags) 在对应fd中,从读缓冲区中拷贝出数据。

(6)ssize_t send(int sockfd, const void *buf, size_t len, int flags) 把fd对应的TCB数据拷贝到写缓冲区中。

(7)int close(int fd) 准备一个FIN包,放到写缓冲区,是否fd。

(8)int connect(int sockfd, const struct sockaddr *addr, socklen_t addrlen) 准备一个SYN包,交给协议栈发送出去,等待三次握手完成后才返回。

二、TCP的三个阶段

2.1 TCP建立连接

TCP连接的建立主要依靠socket()、bind()、listen()、connect()、accept()这几个函数。

2.1.1、TCP的三次握手

示意图:

三次握手在kernel协议栈中进行,那么三次握手是在哪几个函数中发送的呢? 第一次,由connect()函数触发 发起握手,也就是发送syn包到服务端; 第二次,在listen()之后accept()之前,服务器接收到syn包后发送syn&&ack包到客户端; 第三次,客户端发送ack包到服务端完成连接的建立。

TCP报头:

代码语言:javascript

AI代码解释

0 |1 |2 |3 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 +-------------------------------+-------------------------------+ | Source Port | Destination Port | +---------------------------------------------------------------+ | Sequence Number | +---------------------------------------------------------------+ | Acknowledgment Number | +-------+-----------+-+-+-+-+-+-+-------------------------------+ | Header| Reserve |U|A|P|R|S|F| Window | | Length| |R|C|S|S|Y|I| | | | |G|K|H|T|N|N| | +-------------------------------+-------------------------------+ | Checksum | Urgent Pointer | +---------------------------------------------------------------+ | Option | +---------------------------------------------------------------+ | Data | | ... | +---------------------------------------------------------------+
  • SYN:即synchronous,同步。
  • ACK:即acknowledgement,确认。
  • PSH:即push,推送。
  • FIN :即finish,结束。
  • RST:即reset,重置。
  • URG:即urgent,紧急。
  • Sequence Number:是数据包本身第一个字节的序列号。
  • Acknowledge Number:是期望对方继续发送的那个确认数据包的序列号其值一般为接收到的Sequence Number加1。

从报文中可以看出,SYN包最重要的是将SYN位设为1,设置Sequence Number;ACK包最重要的是将ACK位设为1,设置Acknowledgment Number。

半连接队列和全连接队列: 在三次握手中,Linux kener 协议栈会维护两个队列:半连接队列和全连接队列。半连接队列(也叫SYN队列):半连接队列在第一握手中,当客户端发送SYN包到服务端时,服务端的半连接队列会加入一个节点,表示此连接处于半连接状态。全连接队列(也叫ACCEPT队列):全连接队列在第三握手中,当客户端发送ACK包到服务端时,服务端会检查半连接队列中是否存在此连接节点(通过五元组进行查找),如果存在就将此连接节点加入全连接队列中;否则将抛弃此连接。

accpt()函数在三次握手完成后,从全连接队列中取出连接节点,为节点分配socket fd,返回到用户态。那么,accept()函数如何知道全连接队列中有节点呢?当三次握手完成后,全连接队列创建节点的同时会释放一个有连接接入的信号(single或信号量),这个信号决定了accept()函数是否可以从全连接队列中取节点;也决定epoll等IO多路复用器能不能检查这个连接fd是否可读。 在阻塞模式下,accept()函数一直等待信号,直到全连接队列中有节点才返回。 在非阻塞模式下,全连接队列为空accept()函数就返回-1,否则返回socket fd。

在listen()函数有,有一个backlog参数,这个参数表示的是全连接队列的大小还是半连接队列的大小呢?随着TCP协议的不断迭代,backlog参数在不同的版本中代表的含义也不相同;它可以是半连接队列大小,也可以是全连接队列大小,也可以是半连接队列+全连接队列的大小总和。不过,效果不会有太大差异。目前版本中主要表示全连接队列的大小。


https://www.dongchedi.com/article/7594176187270693401
https://www.dongchedi.com/article/7594176654025589273
https://www.dongchedi.com/article/7594174513613636120
https://www.dongchedi.com/article/7594173614161723966
https://www.dongchedi.com/article/7594174316762726937
https://www.dongchedi.com/article/7594174486359114264
https://www.dongchedi.com/article/7594172209028399641
https://www.dongchedi.com/article/7594172028136112665
https://www.dongchedi.com/article/7594170708973158937
https://www.dongchedi.com/article/7594171822011318809
https://www.dongchedi.com/article/7594169237096940057
https://www.dongchedi.com/article/7594169267902857753
https://www.dongchedi.com/article/7594154183356727870
https://www.dongchedi.com/article/7594153895329448472
https://www.dongchedi.com/article/7594153335339647550
https://www.dongchedi.com/article/7594149462356804121
https://www.dongchedi.com/article/7594143242124427801
https://www.dongchedi.com/article/7594143151032959513
https://www.dongchedi.com/article/7594143310076477976
https://www.dongchedi.com/article/7594143261762585112
https://www.dongchedi.com/article/7594143234042380824
https://www.dongchedi.com/article/7594143047861420569
https://www.dongchedi.com/article/7594143261762257432
https://www.dongchedi.com/article/7594113295314305561
https://www.dongchedi.com/article/7594111799855956505
https://www.dongchedi.com/article/7594110342540673598
https://www.dongchedi.com/article/7594110249179578904
https://www.dongchedi.com/article/7594109373581492761
https://www.dongchedi.com/article/7594199698655724056
https://www.dongchedi.com/article/7594197415100793369
https://www.dongchedi.com/article/7594197322150904382
https://www.dongchedi.com/article/7594196529834181145
https://www.dongchedi.com/article/7594196856478368281
https://www.dongchedi.com/article/7594195826612781592
https://www.dongchedi.com/article/7594195431069762072
https://www.dongchedi.com/article/7594195431069270552
https://www.dongchedi.com/article/7594179258625802776
https://www.dongchedi.com/article/7594177577032991257
https://www.dongchedi.com/article/7594179258625409560
https://www.dongchedi.com/article/7594177972484309529
https://www.dongchedi.com/article/7594176579102884414
https://www.dongchedi.com/article/7594177062877839897
https://www.dongchedi.com/article/7594174721596473881
https://www.dongchedi.com/article/7594175192449008153
https://www.dongchedi.com/article/7594173652875444760
https://www.dongchedi.com/article/7594172550394069566
https://www.dongchedi.com/article/7594172678253265470
https://www.dongchedi.com/article/7594171407253275198
https://www.dongchedi.com/article/7594169982704173593
https://www.dongchedi.com/article/7594171805393650201
https://www.dongchedi.com/article/7594171076024812094
https://www.dongchedi.com/article/7594169163703271998
https://www.dongchedi.com/article/7594155915663180313
https://www.dongchedi.com/article/7594152900747100734
https://www.dongchedi.com/article/7594151966180426264
https://www.dongchedi.com/article/7594148426036904473
https://www.dongchedi.com/article/7594143100311192088
https://www.dongchedi.com/article/7594143206229705278
https://www.dongchedi.com/article/7594143047860961817
https://www.dongchedi.com/article/7594143133597205054
https://www.dongchedi.com/article/7594143172075487806
https://www.dongchedi.com/article/7594143133597073982
https://www.dongchedi.com/article/7594142854390759961
https://www.dongchedi.com/article/7594111120601039384
https://www.dongchedi.com/article/7594111426885583384
https://www.dongchedi.com/article/7594112371485393470
https://www.dongchedi.com/article/7594109925660426776
https://www.dongchedi.com/article/7594108812211438104

版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/1/25 3:48:33

iOS个性化定制终极指南:无需越狱打造完全专属iPhone体验

iOS个性化定制终极指南&#xff1a;无需越狱打造完全专属iPhone体验 【免费下载链接】CowabungaLite iOS 15 Customization Toolbox 项目地址: https://gitcode.com/gh_mirrors/co/CowabungaLite 还在为千篇一律的iOS界面感到乏味吗&#xff1f;想要让iPhone真正成为你的…

作者头像 李华
网站建设 2026/1/31 2:14:25

ResNet18优化实战:模型量化与加速的实践

ResNet18优化实战&#xff1a;模型量化与加速的实践 1. 引言&#xff1a;通用物体识别中的ResNet-18价值 在当前AI应用广泛落地的背景下&#xff0c;轻量级图像分类模型成为边缘设备、嵌入式系统和低延迟服务的核心需求。ResNet-18作为深度残差网络中最经典的轻量版本之一&am…

作者头像 李华
网站建设 2026/1/26 16:15:49

DoL-Lyra整合包终极使用手册:5分钟快速精通秘籍

DoL-Lyra整合包终极使用手册&#xff1a;5分钟快速精通秘籍 【免费下载链接】DoL-Lyra Degrees of Lewdity 整合 项目地址: https://gitcode.com/gh_mirrors/do/DoL-Lyra 还在为Degrees of Lewdity游戏的各种Mod安装烦恼吗&#xff1f;DoL-Lyra整合包彻底改变了传统Mod管…

作者头像 李华
网站建设 2026/2/2 10:02:39

深蓝词库转换工具:输入法词库转换的终极解决方案

深蓝词库转换工具&#xff1a;输入法词库转换的终极解决方案 【免费下载链接】imewlconverter ”深蓝词库转换“ 一款开源免费的输入法词库转换程序 项目地址: https://gitcode.com/gh_mirrors/im/imewlconverter 还在为不同输入法间词库格式不兼容而烦恼吗&#xff1f;…

作者头像 李华
网站建设 2026/1/25 19:58:50

E-Hentai漫画批量下载工具的架构设计与技术实现

E-Hentai漫画批量下载工具的架构设计与技术实现 【免费下载链接】E-Hentai-Downloader Download E-Hentai archive as zip file 项目地址: https://gitcode.com/gh_mirrors/eh/E-Hentai-Downloader 在数字漫画收藏领域&#xff0c;E-Hentai Downloader通过创新的技术架构…

作者头像 李华
网站建设 2026/1/25 19:27:46

ResNet18性能测试:不同分辨率图像处理能力

ResNet18性能测试&#xff1a;不同分辨率图像处理能力 1. 引言&#xff1a;通用物体识别中的ResNet-18价值定位 在当前AI视觉应用广泛落地的背景下&#xff0c;轻量级、高稳定性、低延迟的图像分类模型成为边缘设备与本地化服务的核心需求。ResNet-18作为深度残差网络&#x…

作者头像 李华