This content has been marked as final. Show 5 replies
Yes it's a good idea. If the connection aborts, investigate that. It shouldn't with communications every three seconds. The most common cause of a connection reset is writing to a connection that has already even closed by the peer: an application protocol error.
Ok, thanks I thought it. I found out how to check if the connection is still active and the only option (I found) is to write some data to the server and wait for a response. This means that the number of requests will be doubled. Is there some another way how to test if the connection is active without duplicating the communication between client and server?
Edited by: 913214 on 1.3.2012 13:33
The only way to detect a TCP connection failure is to write to it. Some applications add an application ping message to accomplish this, but if you're sending every three seconds you really shouldn't need that: just send the message and catch the exception. In general the only real way to determine the availability of any resource is just to try to use it. You should flush() after every message before reading the reply, and if you have extreme timing requirements there are certain other measures which I will not name here as they are generally abused.
I had one more question on this topic relating to capture and processing exceptions. Communication between client and server I accomplish wit these three classes:
- ClientRequest - represents client request to the server
- Communication - gets ClientRequest and socket connection to the server and sends the request to the server
- ServerResponse - represents response returned from the server
Now If Communication class gets dead connection it can't work and throws an exception. We discussed that I should find out the situation and suitably respond to this situation.
My question is whether the method for reconnection should call:
a) Communication class
b) class that uses the Communication class?
c) somebody else (?)
In case a) there is an advantage that all is in one place and Communication class user does not have to worry about anything beause class will try reconnect itself (if second attempt fails, method throws another exception). The advantage of b) is that the user needs, he can respond to this situation and do some another action.
Well that all depends on whether you want it automatic or manual with a user approval step. Can't answer that for you. It's not really a Java Networking question, it's an application design question.