Is it possible to be aware of a broken TCP connection ?

L

layman

Hello group,
I am using a ServerSocket to accept TCP connections, so once a
connection is established a Socket reference is returned. The Socket
class provides isClosed() and isConnected() method, however it seems
that both of the two methods always returns (false, true)
correspondingly, even if the client already disconnected. But when the
server attempts to write sth. to the socket's output stream, A
"connection reset" exception is thrown, which I DO NOT consider it as a
good way for detecting connection broken or not.
Any other solutions?
Thank you in advance!




layman
 
T

Tony Morris

layman said:
Hello group,
I am using a ServerSocket to accept TCP connections, so once a
connection is established a Socket reference is returned. The Socket
class provides isClosed() and isConnected() method, however it seems
that both of the two methods always returns (false, true)
correspondingly, even if the client already disconnected. But when the
server attempts to write sth. to the socket's output stream, A
"connection reset" exception is thrown, which I DO NOT consider it as a
good way for detecting connection broken or not.
Any other solutions?
Thank you in advance!




layman

This is the nature of TCP.
You can detect a broken connection by a send or recv.
This is often done by sending fake data i.e. ping/pong.
 
T

Thomas Schodt

layman said:
I am using a ServerSocket to accept TCP connections, so once a
connection is established a Socket reference is returned. The Socket
class provides isClosed() and isConnected() method, however it seems
that both of the two methods always returns (false, true)
correspondingly, even if the client already disconnected.

isConnected() and isClosed() tell you what operations *you* have
performed on the socket.
isClosed() will only return true after *you* called close().
But when the server attempts to write something
to the socket's output stream, A "connection reset"
exception is thrown, which I DO NOT consider it as
a good way for detecting connection broken or not.

That is how it is done.
 
O

opalpa

I have this exact same issue. I also get SocketException with
Connection reset. Can I at least depend on
SocketException.getMessage() always returning "Connection reset"?
Because I don't want to interpret other SocketException instances as a
close on the other side and I don't want to miss one that I care about
because it says "Connection reset by peer" or something else. I
believe that the client program which is connecting to my server does a
close(file_descriptor) in C when it wants to close the connection it
has with me. I cannot modify client code.

I also did not know how to interpet isClosed() and isConnected() . Is
there a way I could suggest additional text to the javadoc which did
not add any information to me after I read it?
Cheers! And as always thank you for your thoughts
 
S

Steve Horsley

I have this exact same issue. I also get SocketException with
Connection reset. Can I at least depend on
SocketException.getMessage() always returning "Connection reset"?
Because I don't want to interpret other SocketException instances as a
close on the other side and I don't want to miss one that I care about
because it says "Connection reset by peer" or something else. I
believe that the client program which is connecting to my server does a
close(file_descriptor) in C when it wants to close the connection it
has with me. I cannot modify client code.

You will only get "Connection reset by peer" if the application at the
far end closes, or closes the connection. If you pull the plug, you get
something different, eventually. Whatever the reason, a SocketException
means the connecton has gone.
I also did not know how to interpet isClosed() and isConnected() . Is
there a way I could suggest additional text to the javadoc which did
not add any information to me after I read it?
Cheers! And as always thank you for your thoughts

isConnected() returns true after the socket has made a succesful
connection (ever after).

isClosed() returns true after a succesful call to close().

There is no deeper meaning to either of them. Neither changes state
just because the connection gets broken and throws a SocketException.

Steve
 
O

opalpa

Thanks for clear answers. I'd like to share some code, at the moment
from memory:

class Server implements Runnable {

abstract class SocketEvent { }
class ConnectionClosedByClient extends SocketEvent { }
class MessageParts extends SocketEvent {
.... definition for header, length, content, etc.
}

public void run {
....
boolean closedByClient = false;
while (!closedByClient) {
InputStream input;
.....
try {
SocketEvent event = waitForNextSocketEvent(input);
if (event instanceof ConnectionClosedByClient) {
closedByClient = true;
} else
.... handle events with content ....
} catch (IOException e) {
.... found out about SocketException: Connection reset
.... socket exception gets thrown from InputStream.read
}
}
....
}

SocketEvent waitForNextSocketEvent() throws IOException {
MessageParts mp = new MessageParts();
if (readHeader(input, mp) && readLength(input,mp) &&
readContent(input,mp))
return mp;
else
return new ConnectionClosedByClient();
}

readHeader and readLength and readContent return false if A) blocking
read call resutled in -1 or B) SocketException with message of
"Connection reset" or C) "Connection reset by peer" is thrown ... and
those will be the String values for ever
 

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

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,906
Latest member
SkinfixSkintag

Latest Threads

Top