Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > I want an event when program I'm connected with closes Socket

Reply
Thread Tools

I want an event when program I'm connected with closes Socket

 
 
opalpa@gmail.com
Guest
Posts: n/a
 
      01-12-2005

I've written socket code previously and am confronted with a new
situation: I have a server which manages multiple connections and each
connections can be reused for multiple messages. The client does not
say anything before closing, it just closes. What is the correct way
to determine that client closed socket?

I've looked at java.net.socket 1.5 documentation and it is much too
imprecise for my understanding. Like does isClosed() mean that close()
was called on instance? Does it mean that specific symbols were
received by instance from client?

It seems that more commonly protocols have explicit messages informing
of closing, however in my circumstances changing protocols is not an
option.

Looking forward to replies.

 
Reply With Quote
 
 
 
 
Esmond Pitt
Guest
Posts: n/a
 
      01-13-2005
wrote:

> I've written socket code previously and am confronted with a new
> situation: I have a server which manages multiple connections and each
> connections can be reused for multiple messages. The client does not
> say anything before closing, it just closes. What is the correct way
> to determine that client closed socket?
>
> I've looked at java.net.socket 1.5 documentation and it is much too
> imprecise for my understanding. Like does isClosed() mean that close()
> was called on instance? Does it mean that specific symbols were
> received by instance from client?


isClosed tells you whether *you* have closed *your end*.

If the other end closes, a read will return -1 or an EOFException.

 
Reply With Quote
 
 
 
 
opalpa@gmail.com
Guest
Posts: n/a
 
      01-13-2005
Thanks mate. Will code up a check for -1 and EOFException. I'm now
wondering what happens on a read where no additional bytes are
available at the moment. Does such a read wait... I'll check the docs
right now...

from java.io.InputStream, which is much better javadoc then
java.net.socket in my op:
"This method blocks until input data is available, the end of the
stream is detected, or an exception is thrown"

So I'm all good with the -1 integer or the exception. Thanks for the
info.

This -1 value is a "magic number" right? But it makes it to Java
because C programmers write more socket code? I dunno

Maybe I'll wrap up InputStream into something that pushes to a three
part callback:

public interface InputStreamCallback {
public void info(byte b);
public void endOfFile();
public void exception(IOException e);
}

Cheers

 
Reply With Quote
 
opalpa@gmail.com
Guest
Posts: n/a
 
      01-13-2005
So I see other people can indent code. However do you do it?
Indenting my posts results in unindented code. Is there some markup
language for these posts?

  indent.me();

<pre>
indented
</pre>

<-- spaces here
cannot insert tab into text panel..... hmmm. interesting

 
Reply With Quote
 
Andrew Thompson
Guest
Posts: n/a
 
      01-14-2005
On 13 Jan 2005 07:27:05 -0800, wrote:

> So I see other people can indent code. However do you do it?


There two preferred ways to access Usenet, one is using the
news server of your local ISP with a specifically configured
news-client software such as 40tude Dialog, Gravity or Outlook
Express, amongst many others.

For those with no ISP of their own (like when accessing Usenet
through an internet cafe or college campus) there are also the
web-interfaces to usenet. I notice you are using one in particular
that has been causing a number of problems for both its users and
other participants of usenet - Google groups.

I would be most appreciative if you could lodge a bug report
with Google. Another person who I referred to Google the other
day had their problem quickly and easily solved.

HTH

--
Andrew Thompson
http://www.PhySci.org/codes/ Web & IT Help
http://www.PhySci.org/ Open-source software suite
http://www.1point1C.org/ Science & Technology
http://www.LensEscapes.com/ Images that escape the mundane
 
Reply With Quote
 
opalpa@gmail.com
Guest
Posts: n/a
 
      01-26-2005
Thank you for your thoughtful contributions.

After researching, implementing and playing I expected a -1 return
value, stands for end of file, from a InputStream.read() where the
input stream is gotten from socket instance. InputStream.read() blocks.
In actuality I get "java.net.SocketException: Connection reset" thrown
when client closes the connection.

Here is a couple of questions:

Can I depend on the message content of the exception "Connection reset"
to be the same across Java versions?

Is there a way I can set something on the scoket to have a -1 returned
instead of getting an exception thrown?
Any other thoughts welcome. Cheers

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
in.close() closes out's socket -- is this a bug? Duane Evenson Java 10 07-04-2009 06:35 AM
socket.unbind or socket.unlisten? - socket.error: (48, 'Addressalready in use') Laszlo Nagy Python 1 01-27-2009 05:05 PM
Socket is still connected after Server-Side socket termination. Gordon Beaton Java 23 09-23-2007 12:14 AM
Closing BufferedWriter Stream closes Socket Brett Everton Java 2 06-04-2004 05:44 AM
client socket closes, but server app doesn't see it jm Java 0 04-30-2004 03:16 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57