select.select() 没有捕获套接字上的异常条件?

发布于 2024-10-27 09:25:31 字数 510 浏览 1 评论 0原文

Python 2.7、Windows XP。我有一个向客户端发送消息的服务器。我使用 select 模块来检查套接字是否准备好接收,以及捕获异常情况。我的印象是,如果客户端关闭了套接字, select() 会在异常条件的套接字列表中返回所述套接字,但它似乎没有这样做:

lin, lout, lex = select.select(socklist, socklist, socklist)
for sock in lin: 
    # handle incoming messages
for sock in lout: 
    # send updates
for sock in lex: 
    # shut down server-side objects for particular client

What would be the best way for the server to certain客户端是否仍然连接?服务器并不总是发送数据,所以我不想依赖 socket.send() 来测试客户端是否仍然存在。

Python 2.7, Windows XP. I have a server that sends messages to client(s). I use select module to check for sockets ready to receive, as well as to catch exceptional conditions. I was under the impression that if a client closed a socket, select() would return said socket in the socket list of exceptional conditions, but it doesn't seem to be doing so:

lin, lout, lex = select.select(socklist, socklist, socklist)
for sock in lin: 
    # handle incoming messages
for sock in lout: 
    # send updates
for sock in lex: 
    # shut down server-side objects for particular client

What would be the best way for the server to determine whether the client is still connected? The server is not always sending data, so I would like not to have to rely on a socket.send() to test whether the client is still there.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

岁月静好 2024-11-03 09:25:31

关闭的套接字不是异常(错误)情况。将会发生的情况是,套接字将位于读取列表 (lin) 中,并且当您读取时,您将获得 0 字节。这意味着另一端已关闭套接字。

更新

在正常实践中,您永远不会在例外列表中看到任何内容,可以安全地忽略它。它适用于很少使用的东西,例如带外(OOB)等。

问题更新的答案

可靠且快速地检测到套接字的另一端已消失可能很棘手。如果可靠、始终、及时地检测它很重要,那么您应该使用更高级别的机制,例如 keepalive/heartbeat。

如果客户端干净地关闭了套接字,那么您应该在读取列表中看到该套接字。从套接字读取将返回 0 字节,表示套接字已关闭 (EOF)。

A closed socket is not an exception (error) condition. What will happen is the socket will be in the read list (lin) and when you read you will get 0 bytes. This means the other end has closed the socket.

Update:

In normal practice you will never see anything in the except list and can safely ignore it. It is for rarely used things like out-of-band (OOB) and such.

Answer to question updates:

Reliably and quickly detecting that the other end of the socket has gone away can be tricky. If it's important to detect it reliably, always and in a timely fashion then you should use a higher level mechanism such as keepalive/heartbeat.

If the client does a clean shutdown of the socket, then you should see the socket in the read list. Reading from the socket will return 0 bytes which indicates the socket is closed (EOF).

丘比特射中我 2024-11-03 09:25:31

套接字上“异常情况”的确切定义取决于底层实现。对于 Windows XP,底层实现是 WinSock 选择函数和异常条件包括:

  • 如果处理连接调用(非阻塞),连接尝试失败。
  • OOB 数据可供读取

The exact definition of "exceptional conditions" on a socket depends on the underlying implementation. For Windows XP, the underlying implementation is the WinSock select function and exceptional conditions include:

  • If processing a connect call (nonblocking), connection attempt failed.
  • OOB data is available for reading
紫南 2024-11-03 09:25:31

客户端关闭套接字也不例外。事实上,这绝不是例外。您将在异常情况列表中收到各种错误,但正确关闭的套接字不是其中之一。

A client closing a socket is not exceptional. It is in fact anything but exceptional. You will get various errors in the list of exceptional conditions, but a correctly closed socket is not one of them.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文