我相信如果我们在非阻塞套接字上调用关闭系统调用它会立即返回,那么如何处理响应?是否关闭? 换句话说,在非阻塞套接字上关闭套接字系统调用的行为是什么?
最佳答案
重要的不是套接字的阻塞状态,而是 SO_LINGER
选项。来自 getsockopt(2)
:
SO_LINGER
controls the action taken when unsent messages are queued on socket and aclose(2)
is performed. If the socket promises reliable delivery of data andSO_LINGER
is set, the system will block the process on theclose(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 thesetsockopt()
system call whenSO_LINGER
is requested). IfSO_LINGER
is disabled and aclose(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/