Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / January 2006

Tip: Looking for answers? Try searching our database.

Unable to know if a socket is closed

Thread view: 
oziris - 17 Jan 2006 08:58 GMT
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--
Gordon Beaton - 17 Jan 2006 09:09 GMT
> When I close a socket from a side of it with
>
[quoted text clipped - 15 lines]
>
> 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.

Signature

[  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

oziris - 17 Jan 2006 09:34 GMT
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--
Gordon Beaton - 17 Jan 2006 10:04 GMT
> 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

Signature

[  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

oziris - 17 Jan 2006 10:14 GMT
I'm using the method read(char[], int, int) on an object BufferedReader

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

-o--
Gordon Beaton - 17 Jan 2006 10:25 GMT
> 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

Signature

[  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



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.