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

Discussion in 'Java' started by opalpa@gmail.com, Jan 12, 2005.

  1. Guest

    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.
    , Jan 12, 2005
    #1
    1. Advertising

  2. Esmond Pitt Guest

    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.
    Esmond Pitt, Jan 13, 2005
    #2
    1. Advertising

  3. Guest

    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
    , Jan 13, 2005
    #3
  4. Guest

    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
    , Jan 13, 2005
    #4
  5. 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
    Andrew Thompson, Jan 14, 2005
    #5
  6. Guest

    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
    , Jan 26, 2005
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Tom Wild
    Replies:
    2
    Views:
    1,561
    vMike
    Aug 25, 2003
  2. jm
    Replies:
    0
    Views:
    352
  3. Brett Everton
    Replies:
    2
    Views:
    3,289
    Brett Everton
    Jun 4, 2004
  4. Gordon Beaton
    Replies:
    23
    Views:
    1,924
    Roedy Green
    Sep 23, 2007
  5. Duane Evenson
    Replies:
    10
    Views:
    722
Loading...

Share This Page