Recognizing when a Socket connection goes away.

Discussion in 'Java' started by Steve., Feb 2, 2004.

  1. Steve.

    Steve. Guest

    I'm a C++ programmer trying to get up to speed in Java and possibly
    save my career. ;-) I'm doing some socket programming, basically a
    java client on a windows machine talking to a C++ server on a Solaris
    machine. I managed to kludge things around and get them talking to
    each other using DataInputStream and DataOutputStream. The thing I
    can't figure out now is how to get the Java client to recognize when
    the server connection ends. I tried catching IOException, but that
    doesn't seem to help. Also I tried calling and checking
    getRemoteServerAddress before accessing the Socket.

    Any ideas of a good way to sense a connection loss?
     
    Steve., Feb 2, 2004
    #1
    1. Advertising

  2. On 2 Feb 2004 07:55:21 -0800, Steve. wrote:
    > I'm a C++ programmer trying to get up to speed in Java and possibly
    > save my career. ;-) I'm doing some socket programming, basically a
    > java client on a windows machine talking to a C++ server on a
    > Solaris machine. I managed to kludge things around and get them
    > talking to each other using DataInputStream and DataOutputStream.
    > The thing I can't figure out now is how to get the Java client to
    > recognize when the server connection ends. I tried catching
    > IOException, but that doesn't seem to help. Also I tried calling and
    > checking getRemoteServerAddress before accessing the Socket.
    >
    > Any ideas of a good way to sense a connection loss?


    The only way to detect EOF is to read from the DataInputStream or
    write to the DataOutputStream. The various read() and write() methods
    all have ways of indicating EOF, see the API documentation for
    details.

    /gordon

    --
    [ do not email me copies of your followups ]
    g o r d o n + n e w s @ b a l d e r 1 3 . s e
     
    Gordon Beaton, Feb 2, 2004
    #2
    1. Advertising

  3. Steve.

    Tony Morris Guest

    "Steve." <> wrote in message
    news:...
    > I'm a C++ programmer trying to get up to speed in Java and possibly
    > save my career. ;-) I'm doing some socket programming, basically a
    > java client on a windows machine talking to a C++ server on a Solaris
    > machine. I managed to kludge things around and get them talking to
    > each other using DataInputStream and DataOutputStream. The thing I
    > can't figure out now is how to get the Java client to recognize when
    > the server connection ends. I tried catching IOException, but that
    > doesn't seem to help. Also I tried calling and checking
    > getRemoteServerAddress before accessing the Socket.
    >
    > Any ideas of a good way to sense a connection loss?


    The nature of TCP (not Java) is such that neither end can determine if the
    other has closed until a read/write operation occurs. Many applications get
    around this by sending a ping/pong request/response at intervals.

    If the ping send succeeds and the other end doesn't return a pong within a
    certain interval, the connection has been closed.

    --
    Tony Morris
    (BInfTech, Cert 3 I.T., SCJP[1.4], SCJD)
    Software Engineer
    IBM Australia - Tivoli Security Software
    (2003 VTR1000F)
     
    Tony Morris, Feb 4, 2004
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Tina

    VS.NET Just Goes Away

    Tina, Jun 17, 2005, in forum: ASP .Net
    Replies:
    4
    Views:
    372
    Kevin Spencer
    Jun 20, 2005
  2. arun venkatesan
    Replies:
    3
    Views:
    579
    velthuijsen
    Apr 21, 2004
  3. alex23

    string goes away

    alex23, Apr 1, 2005, in forum: Python
    Replies:
    8
    Views:
    302
    Duncan Booth
    Apr 4, 2005
  4. Andreas Beyer

    Re: string goes away

    Andreas Beyer, Apr 1, 2005, in forum: Python
    Replies:
    3
    Views:
    264
    John J. Lee
    Apr 4, 2005
  5. Michael Chermside

    string goes away

    Michael Chermside, Apr 1, 2005, in forum: Python
    Replies:
    0
    Views:
    251
    Michael Chermside
    Apr 1, 2005
Loading...

Share This Page