Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: How to detect if Socket is closed by remote peer?

  1. #1
    Junior Member
    Join Date
    Feb 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question How to detect if Socket is closed by remote peer?

    Hi,
    I am writing a proxy in Java. I want to know, how one may detect if the server or client has closed its socket? I am using pre-NIO sockets.

    I have used read() method for reading. When I am reading (with read() ) as a client (from the server) if I get -1, does it mean that the server has closed
    the socket?

    In the same way, If I am reading (with read()) from the client and get -1, does it mean that the client has closed the socket?

    What is the best way to detect if a remote peer has closed the socket or not?
    If I use methods like isInputShutdown() or isOutputShutdown(), will that help?

    My main goal is read into a byte array until the remote peer has closed the socket? Any sample code will be much appreciated.

    Bye.


  2. #2
    Member
    Join Date
    Jul 2012
    Posts
    119
    Thanks
    0
    Thanked 19 Times in 19 Posts

    Default Re: How to detect if Socket is closed by remote peer?

    Quote Originally Posted by anis_huq View Post
    Hi,
    I am writing a proxy in Java. I want to know, how one may detect if the server or client has closed its socket? I am using pre-NIO sockets.

    I have used read() method for reading. When I am reading (with read() ) as a client (from the server) if I get -1, does it mean that the server has closed
    the socket?

    In the same way, If I am reading (with read()) from the client and get -1, does it mean that the client has closed the socket?

    What is the best way to detect if a remote peer has closed the socket or not?
    If I use methods like isInputShutdown() or isOutputShutdown(), will that help?

    My main goal is read into a byte array until the remote peer has closed the socket? Any sample code will be much appreciated.

    Bye.
    the -1 just tells you that there's NOTHING more in the "pipeline" and not about the state of the Socket. The Same with ]isInputShutdown() or isOutputShutdown(). Both refer to the state of the In- or Out-pipeline.
    Only the method isClosed() returns the closed state of the socket.

Similar Threads

  1. could anybody fix my problem? resultset is closed
    By kagami in forum What's Wrong With My Code?
    Replies: 2
    Last Post: April 18th, 2012, 08:18 AM
  2. Result set is closed
    By keshav_agrawal89 in forum What's Wrong With My Code?
    Replies: 6
    Last Post: June 11th, 2011, 08:35 AM
  3. [SOLVED] Detect USB signal
    By mine0926 in forum Java Native Interface
    Replies: 2
    Last Post: February 4th, 2011, 07:46 PM
  4. Replies: 3
    Last Post: January 5th, 2011, 08:12 AM
  5. ResultSet is Closed
    By ramayya4u in forum JavaServer Pages: JSP & JSTL
    Replies: 1
    Last Post: May 24th, 2009, 03:54 AM

Tags for this Thread