在我遇到的 BSD 套接字编程上下文中的每个示例和讨论中,似乎将文件描述符设置为非阻塞 I/O 模式的推荐方法是使用 O_NONBLOCK
标志来设置 fcntl()
,例如。
int flags = fcntl(fd, F_GETFL, 0);
fcntl(fd, F_SETFL, flags | O_NONBLOCK);
我在 UNIX 中从事网络编程已经超过十年了,并且一直使用 FIONBIO ioctl()
调用来完成这项工作:
int opt = 1;
ioctl(fd, FIONBIO, &opt);
我从来没想过为什么,就是这么学的。
有人对这两者各自可能的优点作出评论吗?我想可移植性轨迹有所不同,但不知道在多大程度上 ioctl_list(2)
不能代表单个 ioctl
方法的那个方面。