Socket stays open?

I have a very simple console Client Server chat going, but when I close the Client by pressing the x button, the Server still shows that the socket is open by checking socket.isConnected() and if(in == null), neither one of these checks work after I close the client window with x. Anyone have any tips for this?

I have also tried setting the socket.keepAlive(true) and sending a message to the client, but after client closes, I still dont have anything showing the client has closed. No exceptions thrown and no messages from my handler.

Thanks for any help provided.

I use mina and use sessionClosed to determine whether a connection is closed.

Ok, after alot of research I found there are 2 alternatives to handling the Client Closing.

1 - Create a pinger method from the server to the client, the client recieves the ping and sends its own message back to the server, once recieved at the server the variables get reset and after a bit of time the server resends the ping. If the server doesnt recieve the ping response back from the client, the server closes the socket. This is the solution I chose to incorporate.

2 - Use the Runtime Class addShutdownHook() which would monitor the client and when the client shuts down, send a message to server letting server know the client shut down and close the socket on the server appropriately.

Thanks for looking into this and I hope the posting has helped anyone with similiar problems.

thanks Renoria,

The reason I chose to not use any other api’s is to try to keep the sending and recieving of messages as simple as possible in order to use this architecture to build multiplayer games down the road on top of it. I do appreciate your response and if I ever incorporate a fully functioning chat I will look into Mina to use.

Thank you.

In my game the shutdownhook won’t work because theres 2 servers, LoginServer and GameServer. If you’re disconnected from the GameServer you return to the loginserver. If your’e gonna implement this kind of distributed handling you should use either Ping or Mina.

Read up on the java doc and on tcp.

Unless you close the socket on iether side fin never gets send and thus you won’t see anything happening on the other side, that is untill it times out.
If you wait long enough it will work. if you want immediate feedback, clean up after you. Unload/release resources, close the socket manually.

Thanks for the response,

The reason for the post was to handle unexpected client closing, either through client accidently clicking the x, client pc crashing, or lost internet connection.

I am using an intermittent pinging from the server that the client must respond to or get disconnected on the server side and I like how it is working.

Thank you for the response.

From a server point of view this will always be a time out.

You can challange the client for an ack if you haven’t send or recieved data from it for a while. If it doesn’t recieve the data for whatever reason (including automatic resends) the socket will time out. The client doesn’t actually have to ‘respond’. Though you probably don’t need it, in any game your going to send state ‘often’ enough. Though with a chat program I suppose there is an senario where you can end up sending nothing indefinitely.