Unable to know if a socket is closed

O

oziris

Hello !

When I close a socket from a side of it with

<code>
socket.getInputStream().close();
socket.getOutputStream().close();
socket.close();
</code>

it seems to be impossible to detect this closing from the other side.
All the methods

socket.isBound()
socket.isConnected()
socket.isClose()

returns true. And no exception is thrown when I try to read from this
socket.

Does someone ever deal with that stuff?

Thanks.

-o--
 
G

Gordon Beaton

When I close a socket from a side of it with

<code>
socket.getInputStream().close();
socket.getOutputStream().close();
socket.close();
</code>

it seems to be impossible to detect this closing from the other side.
All the methods

socket.isBound()
socket.isConnected()
socket.isClose()

returns true. And no exception is thrown when I try to read from this
socket.

Does someone ever deal with that stuff?

The methods isClosed() etc only tell you the state of the Socket
*object* itself, not the state of the underlying socket, i.e. they
tell you whether you've called the corresponding close() method.

To determine whether a socket connection is still active, you must
attempt to read from it or write to it. Depending on what mechanism
you use, you may get an exception or simply a value indicating EOF.
Read the appropriate documentation.

/gordon.
 
O

oziris

Indeed when the socket is closed the read() method returns -1.

But in my application context this just does mean there is nothing to
read, i.e. I can't conclude my socket has been closed by the other side
with that returned code :-(

Another idea?

Thanks.

-o--
 
G

Gordon Beaton

Indeed when the socket is closed the read() method returns -1.

But in my application context this just does mean there is nothing
to read, i.e. I can't conclude my socket has been closed by the
other side with that returned code :-(

If InputStream.read() returns -1, it means you have reached EOF. It
does not mean there is nothing to read at the moment, it means you
will never be able to read data from the socket again.

Exactly what class and method are you using to read from your socket?

/gordon
 
O

oziris

I'm using the method read(char[], int, int) on an object BufferedReader


BufferedReader m_inputStream =
new BufferedReader(
new InputStreamReader(socket.getInputStream()));

-o--
 
G

Gordon Beaton

I'm using the method read(char[], int, int) on an object BufferedReader

From the API documentation for BufferedReader.read(char[], int, int):

Returns: The number of characters read, or -1 if the end of the
stream has been reached.

With emphasis: the *end* of the stream. There will be no more data on
this stream, ever.

/gordon
 

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,780
Messages
2,569,611
Members
45,270
Latest member
TopCryptoTwitterChannels_

Latest Threads

Top