Recognizing when a Socket connection goes away.

S

Steve.

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?
 
G

Gordon Beaton

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
 
T

Tony Morris

Steve. said:
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)
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,021
Latest member
AkilahJaim

Latest Threads

Top