This discussion is archived
3 Replies Latest reply: Apr 23, 2007 5:23 PM by EJP RSS

No exception when writing/flushing data to a socket having lost connection

807597 Newbie
Currently Being Moderated
Hi,

I am using a client socket to send messages to a commercial application.

The client socket is initialized and the connection to the server established at the startup of the client application (keep alive and tcp no delay are set to true).

In case of loss of connection I have added an automatic reconnection mechanism. The problem is that no exception is raised when writing / flushing the socket. It is only at the 2nd writing/flusing attempt that the IOException is raised.

Here is the state of the established connection

TCP wt004-0026:3002 wt004-0026.corp.local:4116 ESTABLISHED
TCP wt004-0026:4116 wt004-0026.corp.local:3002 ESTABLISHED

After the server application is shutdown, the state of the connection changed to:

TCP wt004-0026:3002 wt004-0026.corp.local:4116 FIN_WAIT_2
TCP wt004-0026:4116 wt004-0026.corp.local:3002 CLOSE_WAIT

As soon as the write/flush operations are performed, the state is clean again.

I saw that people encounter similar behavior but I didn't see a way to solve/avoid it. Any idea?

Thanks in advance for your help.

Fred
  • 1. Re: No exception when writing/flushing data to a socket having lost connect
    EJP Guru
    Currently Being Moderated
    There is no way around this. The kernel TCP/IP stack does buffering so the problem won't usually be noticed for a write or two. Nothing you or Java can do about it.
  • 2. Re: No exception when writing/flushing data to a socket having lost connect
    807597 Newbie
    Currently Being Moderated
    Hi:

    I'm having the same problem here and I have been reading alot from everywhere. And I'm so confused. What I find is ppl are talking about socket connection can be detected thru write() or read() thru the socket:

    But how come here you said that this is a Java problem that write() on a broken socket will not given you the Java exceptoin?

    Here are what I understand from all my readings:
    1. read() on a broken socket will return you a -1 / null / IOException
    2. write() on a broken socket will return you a IOException

    And ppl also mentions it would be different if the server has closed the socket normally... eg. socket.close() or someone just unpluged the server or network, each will have a different behaviour to the client....
    SO SO confused.

    Please Please help. So what would be the best way to dectect broken socket to the server?

    Thanks.
  • 3. Re: No exception when writing/flushing data to a socket having lost connect
    EJP Guru
    Currently Being Moderated
    But how come here you said that this is a Java
    problem that write() on a broken socket will not
    given you the Java exceptoin?
    But who said that?
    1. read() on a broken socket will return you a -1 /
    null / IOException
    Only if the connection got an orderly close from the other end, or it was reset at the other end for some reason and you have subsequently written some data. Not to be relied on.
    2. write() on a broken socket will return you a
    IOException
    Eventually, after some buffers have been flushed. You can't rely on the first write to a broken connection doing it but you can rely on it happening eventually, and after not many extra writes.
    And ppl also mentions it would be different if the
    server has closed the socket normally... eg.
    socket.close() or someone just unpluged the server or
    network, each will have a different behaviour to the
    client....
    SO SO confused.
    Yes, see above. If you read from a properly closed connection you get -1/null/EOF. If you write to it you get the same exception as above.
    Please Please help. So what would be the best way to
    dectect broken socket to the server?
    Write to it.
    Thanks.