1

I want to differentiate the exceptions for server connection close on a client socket like if the server socket closes due to idle timeout that reason has to be generated on Client side as idle time out exception. Similarly for connection failure as connection failure exception and so on. Please help me to resolve this problem. Thanks in advance.

Zaki
  • 6,997
  • 6
  • 37
  • 53
Rocky
  • 129
  • 2
  • 12
  • 1
    See http://stackoverflow.com/questions/151590/java-how-to-detect-a-remote-side-socket-close & http://stackoverflow.com/questions/155243/why-is-it-impossible-without-attempting-i-o-to-detect-that-tcp-socket-was-grac – Zaki Sep 02 '12 at 16:15

2 Answers2

1

The best way is for the server to send you a message saying why it closed the connection. The server will not close a connection which has been idle by default.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
  • Thanks Peter for your answer. Please provide me an example or a method how to do that. – Rocky Sep 03 '12 at 04:14
  • How you do this depend on your protocol. Say you have a protocol which sends a message type and message length, you could add a type which means DISCONNECTED_DUE_TO_IDLE_TIMEOUT and include in the body, a text message. – Peter Lawrey Sep 03 '12 at 07:16
  • I am using apache mina framework for server client. So which protocol should I use for that. Please tell me about how to use that protocol syntax. Thanks in advance – Rocky Sep 03 '12 at 08:26
  • You can use any protocol with Mina, it is just a transport. Do you have a protocol currently? What are you using the connections for? – Peter Lawrey Sep 03 '12 at 08:29
  • I am presently not using any protocol and even I am not aware of such thing does exist. Please tell me about those protocols and explain how to use them. Or else at least provide a link so that I can refer them. Thanks Peter for all your help and support – Rocky Sep 03 '12 at 09:21
  • If you are sending any data, you are using a protocol. It may be a very simple protocol, but the only way not to have one is to not open a connection. The protocol is the structure for the data you send. http://en.wikipedia.org/wiki/Communications_protocol – Peter Lawrey Sep 03 '12 at 09:27
  • I understood about protocol and implemented that and its working. But I have a small question ie before idle timeout connection closes the client socket if send a message to client then there will be some data flow between them so idle timeout method should not be excuted and it should start from first but it is not happening like that may I know the reason for that? – Rocky Sep 03 '12 at 10:08
  • You would send the idle timeout message before closing the connection. There is no messages you would send after that. I would have the timeout occur from the last message received and add a heartbeat message to prevent the timeout occurring when the connection is good. – Peter Lawrey Sep 03 '12 at 10:12
  • 1
    Thanks for all your help Peter. I understood that concept clearly – Rocky Sep 03 '12 at 10:27
1

If the server closes the socket properly, there is only one possible exception at the client, not a series of different exceptions that the client has to catch and figure out. There is just an EOS indication: read() returns -1, readLine() returns null, readXXX() throws EOFException for any other X.

If the connection is dropped due to a network fault, it is possible for the client to get an IOException with the text 'connection reset'. If the connection stays up but the server isn't sending data and the client has a read timeout, the client will get a SocketTimeoutException. Neither of these implies that the server has closed the connection (although it is possible for the server to cause a connection reset, by means which I will not document here).

If you need to know why the server closed the socket, it will have to tell you via a message.

user207421
  • 305,947
  • 44
  • 307
  • 483
  • Thanks dude for your answer. By using which method we can send a message from server to client please let me no that.... – Rocky Sep 03 '12 at 04:23
  • @Rocky The same method you send any other message. How are you sending message currently? – Peter Lawrey Sep 03 '12 at 08:30
  • I am using session.write method to send data. In server idle conn timeout if we send any message to client then the timeout method will not be excuted as there will be some data flow between them. So the timeout method starts from first and idle connection time out never occurs. Thanks Peter for ur help & support – Rocky Sep 03 '12 at 10:03