一、函数原型
DESCRIPTION:
* The accept function is used with connection-based socket types
* (SOCK_STREAM, SOCK_SEQPACKET and SOCK_RDM). It extracts the first
* connection request on the queue of pending connections, creates a new
* connected socket with mostly the same properties as 'sockfd', and
* allocates a new socket descriptor for the socket, which is returned. The
* newly created socket is no longer in the listening state. The original
* socket 'sockfd' is unaffected by this call. Per file descriptor flags
* are not inherited across an accept.
*
int accept(int sockfd,
FAR struct sockaddr *addr,
FAR socklen_t *addrlen)
TCP客户端依次调用socket()、connect()之后就想TCP服务器发送了一个连接请求。TCP服务器监听到这个请求之后,就会调用accept()函数取接收请求,这样连接就建立好了。
二、使用顺序
一图胜千言:

- 第一个参数为服务器的soc

本文介绍了Linux下socket编程中accept函数的使用,包括函数原型、使用顺序、使用场景和实例。接受TCP连接请求时,服务器通过调用accept获取新的描述字,以建立与客户端的连接。结合select,服务器可以高效监听多个端口。
最低0.47元/天 解锁文章
802

被折叠的 条评论
为什么被折叠?



