5 Replies Latest reply on Mar 3, 2012 11:53 PM by EJP

    Keep opened socket connection?


      My Java application communicates with server on average every 3 seconds. I wonder if it is a good idea to keep an opened socket connection to the server and don't open new connection for each request. The problem with this is that if in the meantime is connection aborted, java.net.SocketException is throwned.

      What do you think about keeping opened connection? Is appropriate to work with opened connections or to open new connection for each new request? How do you solve in your applications situation when communication is irregular but frequent.

        • 1. Re: Keep opened socket connection?
          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.
          • 2. Re: Keep opened socket connection?
            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
            • 3. Re: Keep opened socket connection?
              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.
              • 4. Re: Keep opened socket connection?
                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.

                • 5. Re: Keep opened socket connection?
                  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.