2

I have some pretty basic old school socket related code used within a tomee servlet to connect to another servlet on another server.

The problem is that it appears that connections are being dropped and the "client" side is never released. No exception, EOF, etc appears.

Is this possible?

Here is the boiler plate code:

private Object sendObject(URL servlet, Serializable obj) throws Exception
{
    HttpURLConnection connection = null;

    InputStream inputstream = null;
    ObjectOutputStream objectoutputstream = null;
    ObjectInputStream result = null;
    Object payload = null;

    try
    {
        //  Set the HostnameVerifier regardless of whether SSL is enabled.
        HostnameVerifier hv = new HostnameVerifier()
        {
            public boolean verify(String urlHostName, SSLSession session)
            {
                return true;
            }
        };
        HttpsURLConnection.setDefaultHostnameVerifier(hv);

        connection = (HttpURLConnection) servlet.openConnection();

        // Prepare for both input and output
        connection.setDoInput(true);
        connection.setDoOutput(true);

        // Turn off caching
        connection.setUseCaches(false);

        connection.setRequestMethod("POST");

        // Set the content type to be application/x-java-serialized-object
        connection.setRequestProperty("Content-Type", "application/x-java-serialized-object");

        setupHeaderAttributes(getHttpHeaders());

        setupSessionCookies(getHttpHeaders());

        // Load/add httpHeaders
        addHeadersToConnection(connection, getHttpHeaders());

        // Write the serialized object as post data
        objectoutputstream = new ObjectOutputStream(connection.getOutputStream());
        objectoutputstream.writeObject(obj);
        objectoutputstream.flush();

        // Get ready to receive the reply.
        inputstream = connection.getInputStream();
        setHttpStatus(connection.getResponseCode());

        if (getHttpStatus() == HttpURLConnection.HTTP_OK)
        {
             ...

So the question is... on a really crappy WAN is it possible for a java connection to have a connection that will never terminate? Assume for the moment that the "server" would normally always return and the network spans a very wide area network with all the associated problems (dropped connections).

D-Klotz
  • 1,973
  • 1
  • 15
  • 37
  • 1
    Sure. TCP half-connections can last forever. – Martin James Aug 12 '15 at 18:48
  • @MartinJames thanks. I'm curious how this occurs but more importantly how to avoid it. Any advice on further reading is appreciated. – D-Klotz Aug 12 '15 at 18:55
  • It happens when the server just goes away, (eg. power fail, cleaner unplugs rack to plug in polisher, insane developer fills server rack with buckshot). If the client is not exchanging data with the server, it will never know that the server has expired. Typically, the TCP keepalive feature or an application-level protocol that allows for occasional polling of the peer is used to detect servers that are nailed to their perch. – Martin James Aug 12 '15 at 19:01
  • I found this link which seems relevant: http://stackoverflow.com/questions/1480236/does-a-tcp-socket-connection-have-a-keep-alive In my case the initial event is a query, so if the connection is silently dropped, then I won't be sending any other data to force an exception. I'd have to hope that a shorter "keep-alive" would do the trick... or rewrite the old mechanism to have shorter communications, constantly asking, "are you there?". Hopefully I won't have to do that. – D-Klotz Aug 12 '15 at 19:12
  • 1
    You are using an `HttpURLConnection` (or `Https`) not a socket connection. HTTP can (with some exceptions) do a series of HTTP requests and responses over a single TCP connection; in between it may sit idle. If the TCP connection terminates *during* an HTTP request, it should indeed get an exception or at least EOF, although I agree *server* failure may go undetected. OTOH server *close* should get through even a pretty bad network; FIN is acked and retried like normal TCP data. – dave_thompson_085 Aug 14 '15 at 02:15

0 Answers0