linux - 如何关闭非阻塞套接字?

标签 linux sockets freebsd nonblocking

我相信如果我们在非阻塞套接字上调用关闭系统调用它会立即返回,那么如何处理响应?是否关闭? 换句话说,在非阻塞套接字上关闭套接字系统调用的行为是什么?

最佳答案

重要的不是套接字的阻塞状态,而是 SO_LINGER 选项。来自 getsockopt(2) :

SO_LINGER controls the action taken when unsent messages are queued on socket and a close(2) is performed. If the socket promises reliable delivery of data and SO_LINGER is set, the system will block the process on the close(2) attempt until it is able to transmit the data or until it decides it is unable to deliver the information (a timeout period, termed the linger interval, is specified in seconds in the setsockopt() system call when SO_LINGER is requested). If SO_LINGER is disabled and a close(2) is issued, the system will process the close in a manner that allows the process to continue as quickly as possible.

也就是说,在 SO_LINGER 启用的情况下,来自 TCP 套接字上的 close(2) 的错误将意味着内核无法在延迟间隔内传送数据(不包括其他文件描述符无效等错误)。随着挥之不去的残疾 - 你永远不会知道。另见 The ultimate SO_LINGER page, or why is my tcp not reliable .

关于linux - 如何关闭非阻塞套接字?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6418277/

相关文章:

python - 如何设置默认的python库路径

linux - 如何将命令行脚本包装到 ./sh 文件

linux - 使用排序从列表中删除行,grep LINUX

java - AIX 环境 - TCP 端口 RecvQ 数据增加但应用程序不消耗

c# - 如何从socket连续接收数据?

c++ 代码在 FreeBSD 上没有捕获异常

使用 DBI 模块和 Oracle 8 的 Perl 脚本停止处理

c++ - 如何检查给定进程在运行时加载了哪些共享库?

linux - 如何使用来自外部服务器 linux 的数据启动本地程序

.net - 有关与 .Net 的 ActionScript 套接字通信的帮助