如何关闭非阻塞套接字? [英] How to close a non-blocking socket?

查看:123
本文介绍了如何关闭非阻塞套接字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I believe that if we call close system call on a non-blocking socket it returns immediately, then how to handle the response? whether it is closed or not? in other words what is the behavior of the socket system call close on a non-blocking socket?

推荐答案

如果我们在上调用close系统调用 返回的非阻塞套接字 立即

if we call close system call on a non-blocking socket it returns immediately

套接字始终处于关闭状态:连接可能仍在写入对等端.但是您的问题体现了一个谬论:如果您在 any 套接字上调用close(),它将立即返回.关闭和写入套接字是异步的.您可以按照其他答案使用SO_LINGER进行控制,尽管我怀疑这仅适用于阻止模式.如果那是您需要执行的操作,则可能应该在使用正SO_LINGER关闭之前将套接字重新置于阻塞模式.

The socket is always closed: the connection may still be writing to the peer. But your question embodies a fallacy: if you call close() on any socket it will return immediately. Closing and writing to a socket is asynchronous. You can control that with SO_LINGER as per the other answer, although I suspect that only applies to blocking mode. Probably you should put the socket back into blocking mode before closing with a positive SO_LINGER if that's what you need to do.

这篇关于如何关闭非阻塞套接字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆