
设置 socket 为非阻塞模式的三种方法
- 创建socket的时候,指定socket是异步的,在type的参数中设置SOCK_NONBLOCK标志即可。
1
2int (int domain, int type, int protocol);
int sockfd = socket(AF_INET, SOCK_STREAM | SOCK_NONBLOCK, 0);
- 使用fcntl函数
1
fcntl(sockfd, F_SETFL, fcntl(sockfd, F_GETFL, 0) | O_NONBLOCK);
- 使用ioctl函数
1
ioctl(sockfd, FIONBIO, 1);




近期评论