没有检测发送或接收插座挂断? [英] Detect socket hangup without sending or receiving?

查看:107
本文介绍了没有检测发送或接收插座挂断?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在写,可以采取15秒以上开始产生一定的请求的响应的主体TCP服务器。有些客户喜欢,如果响应时间超过几秒钟即可完成收于他们端的连接。

I'm writing a TCP server that can take 15 seconds or more to begin generating the body of a response to certain requests. Some clients like to close the connection at their end if the response takes more than a few seconds to complete.

由于产生的反应是非常CPU密集型的,我倒是preFER停止任务客户端关闭连接的瞬间。在present,我不觉得这一点,直到我发的第一个有效载荷和接收各种挂断错误。

Since generating the response is very CPU-intensive, I'd prefer to halt the task the instant the client closes the connection. At present, I don't find this out until I send the first payload and receive various hang-up errors.

我如何检测对端已​​经关闭,不发送或接收任何数据的连接?这意味着的recv 所有数据保留在内核中,或发送,没有数据实际传输。

How can I detect that the peer has closed the connection without sending or receiving any data? That means for recv that all data remains in the kernel, or for send that no data is actually transmitted.

推荐答案

我已经与有用于发送和接收单独的TCP连接的设备通信时经常发生的问题。基本的问题是TCP堆栈通常不会告诉你一个套接字关闭时,你只是想读 - 你必须尝试和写入得到告诉链接的另一端被放弃了。一定程度上,这是TCP只是如何设计的(阅读是被动的)。

I've had a recurring problem communicating with equipment that had separate TCP links for send and receive. The basic problem is that the TCP stack doesn't generally tell you a socket is closed when you're just trying to read - you have to try and write to get told the other end of the link was dropped. Partly, that is just how TCP was designed (reading is passive).

我猜测布莱尔的回答工作在那里的插座已在另一端很好地关闭的情况下(即他们发送的正确断开连接的消息),但不是在的情况下,另一端也不客气刚刚停止监听

I'm guessing Blair's answer works in the cases where the socket has been shut down nicely at the other end (i.e. they have sent the proper disconnection messages), but not in the case where the other end has impolitely just stopped listening.

是否有你的消息开始一个相当固定格式的标题,您可以通过发送开始,在此之前,整个反应是准备好了?例如一个XML文档类型?此外,你能够与消息在某些点上发送一些额外的空格就完事了 - 只是一些空数据可以输出,以确保插座仍然是开放的。

Is there a fairly fixed-format header at the start of your message, that you can begin by sending, before the whole response is ready? e.g. an XML doctype? Also are you able to get away with sending some extra spaces at some points in the message - just some null data that you can output to be sure the socket is still open?

这篇关于没有检测发送或接收插座挂断?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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