简体   繁体   English

Java多客户端服务器套接字。 管道破损检测

[英]Java Mult-Client Server Socket. Pipe broken detection

I am create a server solution which have many client. 我正在创建一个具有许多客户端的服务器解决方案。 Use a arraylist the store the user information and the socket. 使用数组列表存储用户信息和套接字。 Broadcast messaging... 广播消息...

On the client side, 在客户端,

When the server is close, a exception will be throw and I can catch the exception and do the corresponding action. 当服务器关闭时,将引发异常,我可以捕获该异常并执行相应的操作。

But on the server side, 但是在服务器端,

When a client offline, the server will not throw a exception. 当客户端脱机时,服务器将不会引发异常。 Do there is any method to achieve. 有没有什么方法可以实现。

I am now doing this with sending a boardcast message every 10 second to see if the connection is health or not. 我现在通过每10秒发送一次板播消息来执行此操作,以查看连接是否正常。 Instead of the shorten the interval between health-checking boardcast messgae. 而不是缩短健康检查董事会广播消息之间的时间间隔。 If there is any method to do so such as setListener? 是否有诸如setListener之类的方法?

There are a lot of solutions for this type of connection in java (like Apache Mina), but that being said unless the client sends a message informing the server that it is about to disconnect there is no other way to tell unless the heartbeat between the server and client fails, which it sounds like you have implemented. 在Java中有很多针对这种类型的连接的解决方案(例如Apache Mina),但是除非客户端发送一条消息通知服务器即将断开连接,否则除非在服务器之间的心跳,否则没有其他方法可以告知。服务器和客户端失败,这听起来像您已实现。 You can see the issue with using a Listener as a fail safe solution because if client fails it will never let the server know and you will have an orphaned session. 您可以看到使用侦听器作为故障安全解决方案的问题,因为如果客户端失败,它将永远不会让服务器知道,并且您将有一个孤立的会话。

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

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