简体   繁体   中英

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. Instead of the shorten the interval between health-checking boardcast messgae. If there is any method to do so such as 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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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