首先什么是accept?以下是常见的接收网络请求的伪代码
int main()
{/*Step 1: 创建服务器端监听socket描述符listen_fd*/ listen_fd = socket(AF_INET, SOCK_STREAM, 0);/*Step 2: bind绑定服务器端的IP和端口,所有客户端都向这个IP和端口发送和请求数据*/ bind(listen_fd, xxx);/*Step 3: 服务端开启监听*/ listen(listen_fd, 128);/*Step 4: 服务器等待客户端的链接,返回值cfd为客户端的socket描述符*/ cfd = accept(listen_fd, xxx);/*Step 5: 读取客户端发来的数据*/n = read(cfd, buf, sizeof(buf));
}
以上代码中的accept通常是调用linux accept方法,以下是该方法的描述
It extracts the first connection request on the queue of pending connections for the listening socket, sockfd, creates a new connected socket, and returns a new file descriptor referring to that socket. The newly created socket is not in the listening state. The original socket sockfd is unaffected by this call.
也就是说实际上是从已经建立的连接队列中取出第一个连接,并返回指向该soccer的文件描述符
那么实际上accept作用在已经三次握手之后了,那么自然能建立连接
Ref
- https://man7.org/linux/man-pages/man2/accept.2.html