、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