简体   繁体   English

Java TCP Socket等待空闲吗?

[英]Java TCP Socket wait for idle?

I'm not sure if this is more a Java question or TCP question or both: 我不确定这是否是Java问题或TCP问题,或两者兼而有之:

I have some server code using Apache Mina that runs a server listening for TCP on a given socket. 我有一些使用Apache Mina的服务器代码,该代码运行在给定套接字上侦听TCP的服务器。 The handler I have extends IoHandlerAdapter. 我拥有的处理程序扩展了IoHandlerAdapter。 I'm using this to connect a camera to the Java server over HTTP/1.1 我正在使用它通过HTTP / 1.1将相机连接到Java服务器

The problem is: if I make the connection and then completely disconnect the camera (either pull power or pull network), I have to wait until the sessionIdle method is called to detect that the session is now dead (and then explicitly close the session from the server side). 问题是:如果我建立了连接,然后完全断开了摄像机的连接(拉电源或拉网络),则必须等到调用sessionIdle方法来检测到该会话现在已死(然后从关闭该会话)服务器端)。

My question is: Isn't there any way to know that the TCP session was instantly broken by the client? 我的问题是:难道没有办法知道客户端立即中断了TCP会话吗? Or is this just the way that TCP sessions / Mina works? 还是这只是TCP会话/ Mina的工作方式?

Ideally, sessionIdle would only be for cases where the TCP socket hasn't died but the client (camera) has stopped talking over the socket... and some other mechanism would catch when the socket is actually killed (by the client/network). 理想情况下,sessionIdle仅适用于TCP套接字尚未终止但客户端(摄像机)已停止通过套接字进行通信的情况……并且当套接字被客户端(或网络)杀死时,某些其他机制会捕获。

NOTE: I am overriding exceptionCaught() but I don't see that being called in the case where I either unplug power or network. 注意:我覆盖了exceptionCaught(),但是在拔掉电源或网络的情况下却看不到该调用。 It just sits until the idle time then calls sessionIdle(). 它一直坐着直到空闲时间再调用sessionIdle()。

Thanks! 谢谢!

Unfortunately, the abrupt removal of one end of a connection is not immediately detectable since the end being removed does not have a chance to notify the other end of the connection. 不幸的是,无法立即检测到连接一端的突然删除,因为被删除的一端没有机会通知连接的另一端。

There is no string (so to speak) connecting two ends of a TCP connection, instead there is just an agreement on connection state. TCP连接的两端没有连接字符串(可以这么说),而是仅在连接状态上有协议。 The result being that until a timeout is hit, there is no way to know that the other end of the connection has disappeared unless that end of the connection sends a notification of its intent to disconnect. 结果是,除非达到超时,否则无法知道连接的另一端已经消失,除非该连接的另一端发送了有关断开连接的意图的通知。

As an analogy, imagine you are carrying on a conversation with someone in another room, whom you cannot see. 打个比方,假设您正在与另一个房间的某人进行对话,而您却看不见。 When do you consider them to be no longer there? 您何时认为它们不再存在? If they tell you they are leaving, and then you hear the door slam shut, it would be reasonable to assume that they have left. 如果他们告诉您他们要离开,然后您听到门猛然关上,则可以合理地假设他们已经离开。 However, if they just fail to answer a question you would probably repeat the question, maybe a little louder this time. 但是,如果他们只是不能回答问题,您可能会重复该问题,这次可能会更大声。 Then you might wait a little bit, call their name, maybe wait a little bit more for a response, and then perhaps finally walk over to the other room to see if they are still there or not. 然后,您可以稍等片刻,给他们打电话,或者再等一会儿,然后最后走到另一个房间,看看他们是否还在。

This is what TCP basically does (especially if you are sending keep alives), except it has no way to walk over to the other room and can only rely on timeout thresholds being hit to indicate that the other party has left. TCP基本上就是这样做的(特别是如果您要发送保持活动),除了它没有办法走到另一房间并且只能依靠超时阈值来表明另一方已经离开之外。 So when a timeout is hit with no response from the other side, the assumption is made that they have left without telling you. 因此,如果在对方没有响应的情况下发生超时,则假定他们离开了而没有告诉您。

This is why when the other side of your connection suddenly disappears you have to rely on the timeout to tell you so, since there is simply no other way to know. 这就是为什么当连接的另一端突然消失时,您必须依靠超时来告诉您的原因,因为根本没有其他方法可以知道。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM