0

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?

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

6 Comments

There is no any onClose method on client side socket? Or is it prossible to implements it? Thank you
You would have to implement it. Like I said it still wouldn't be perfect. What if the power shuts off on the client device? Its not going to tell the server anything the server will then throw an error on the next heartbeat.
I know it will not tell the server anything. I am going to implements myself. But if there is a exception throw, I can call a callback method inside the catch block. But If I force shut down the device or the program. There is not error shown in client too. So the only solution is to throw an error on the next heartbeat in server? BTW thank :D
Ok sounds like you have your bases covered with the Server los of connection to the client. On the client side you could set a timeout in which to close the connection if you don't hear from the server. So maybe that timeout is 30 seconds if you have a ping every 10 seconds. Push Mail works similarly: technet.microsoft.com/en-us/library/aa997252(v=exchg.150).aspx.
I am now doing is the method you said.Ping every 10 second. but the program is used in primary school. About 300 client keep on pinging the server every 10s. And the network of the school is very bad. I want to decrease the use of bandwidth as possible as I can. I know ping not spend too much but I was just finding a better way. When the socket dispose , a method will call and I can set some exception action in it :D Thank you btw.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.