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

O

opalpa

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.
 
E

Esmond Pitt

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.
 
O

opalpa

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
 
O

opalpa

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
 
A

Andrew Thompson

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
 
O

opalpa

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
 

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,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top