How to time out a read from socket

Discussion in 'Java' started by Ed, Oct 12, 2004.

  1. Ed

    Ed Guest

    Guys,

    I am looking to read from a socket and keep reading until I receive a
    certain set of characters. However in an "error" situation these
    characters never arrive, the socket "dries up" and I need to be able
    to spot this.

    I have a loop as follows:


    while((c = m_reader.read()) != -1)
    {
    message.append((char)c);
    if (message.toString().endsWith(endOfResponseString))
    {
    break;
    }
    }

    Obviously if the server misbehaves, the m_reader.read() blocks and
    everything hangs.

    What I want is some way of "timing out" the loop after a period of
    inactivity.

    The server is a remote IP server so a message can be sent in separate
    "chunks". m_reader is a BufferedReader but that can be changed if it
    helps achieve the desired affect.

    Any ideas?

    Cheers

    Ed
     
    Ed, Oct 12, 2004
    #1
    1. Advertisements

  2. On 12 Oct 2004 05:08:19 -0700, Ed wrote:
    > I have a loop as follows:
    >
    > while((c = m_reader.read()) != -1)
    > {
    > message.append((char)c);
    > if (message.toString().endsWith(endOfResponseString))
    > {
    > break;
    > }
    > }
    >
    > Obviously if the server misbehaves, the m_reader.read() blocks and
    > everything hangs.
    >
    > What I want is some way of "timing out" the loop after a period of
    > inactivity.


    It looks to me like the remote process isn't closing his end of the
    connection as maybe it should when an error occurs, or you'd get EOF
    here.

    Anyway you can use Socket.setSoTimeout() to limit the time you block
    while reading. Or use java.nio.Selector.select() with a timeout,
    *before* reading from the corresponding SocketChannel.

    /gordon


    --
    [ 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
     
    Gordon Beaton, Oct 12, 2004
    #2
    1. Advertisements

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. Ed
    Replies:
    2
    Views:
    553
    Alan Meyer
    Oct 1, 2004
  2. flamesrock
    Replies:
    8
    Views:
    818
    Hendrik van Rooyen
    Nov 24, 2006
  3. Kid

    Socket time out question

    Kid, Jan 4, 2008, in forum: Java
    Replies:
    3
    Views:
    6,444
    Richard Maher
    Jan 5, 2008
  4. Laszlo Nagy
    Replies:
    1
    Views:
    7,241
    Mark Wooding
    Jan 27, 2009
  5. Steve Holden
    Replies:
    1
    Views:
    1,006
  6. Johann Schlamp

    Socket creation/write/read time

    Johann Schlamp, Mar 17, 2009, in forum: C Programming
    Replies:
    4
    Views:
    1,878
    Johann Schlamp
    Mar 17, 2009
  7. J
    Replies:
    2
    Views:
    848
  8. Replies:
    19
    Views:
    1,690
    Nigel Wade
    Mar 15, 2012
Loading...