ReadLine from a network stream hangs if it is empty

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Scott

    ReadLine from a network stream hangs if it is empty

    Hi guys,

    If I try to call read(), readline(), readtoend() and
    there is nothing to read (from a never ending loop for
    example) the program seems to continue but it exits the
    loop for no apparent reason.

    We also can't check the stream for the length, as the
    network stream doesn't support seek operations

    MSDN reckons that the functions should return null if
    there is nothing to read but it doesn't.

    Can anybody post some example code that shows how to read
    the entire contents of a network stream and handling if
    there isn't anything in it?

    Thanks in advance,


    Scott
  • Jon Skeet [C# MVP]

    #2
    Re: ReadLine from a network stream hangs if it is empty

    Scott <scott@discussi ons.microsoft.c om> wrote:[color=blue]
    > If I try to call read(), readline(), readtoend() and
    > there is nothing to read (from a never ending loop for
    > example) the program seems to continue but it exits the
    > loop for no apparent reason.
    >
    > We also can't check the stream for the length, as the
    > network stream doesn't support seek operations
    >
    > MSDN reckons that the functions should return null if
    > there is nothing to read but it doesn't.[/color]

    No, ReadLine should return null if the stream has been *closed*. It
    should block if there's just no data ready.
    [color=blue]
    > Can anybody post some example code that shows how to read
    > the entire contents of a network stream and handling if
    > there isn't anything in it?[/color]

    Your protocol should indicate when all the data has been read, either
    by stating in advance how much there is to read, or by indicating the
    "end of data". Alternatively, if the client isn't expected to do
    anything else afterwards, the server can close the socket.

    --
    Jon Skeet - <skeet@pobox.co m>
    Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

    If replying to the group, please do not mail me too

    Comment

    • Scott

      #3
      Re: ReadLine from a network stream hangs if it is empty

      Hi Jon,

      Thanks for the info, I guess my real problem is that I
      don't know what the correct method is to check if there
      is any data in the stream without causing it to screw up
      (if there is no data ready)

      Is there a way to check if readline has anything in it
      before trying to get data from it? because currently if
      we try and get the contents of readline() into a variable
      and readline is empty, it exits our 'never ending' loop
      and processing stops completely, but we dont get any
      errors thrown at us

      Cheers,

      Scott

      [color=blue]
      >-----Original Message-----
      >Scott <scott@discussi ons.microsoft.c om> wrote:[color=green]
      >> If I try to call read(), readline(), readtoend() and
      >> there is nothing to read (from a never ending loop for
      >> example) the program seems to continue but it exits[/color][/color]
      the[color=blue][color=green]
      >> loop for no apparent reason.
      >>
      >> We also can't check the stream for the length, as the
      >> network stream doesn't support seek operations
      >>
      >> MSDN reckons that the functions should return null if
      >> there is nothing to read but it doesn't.[/color]
      >
      >No, ReadLine should return null if the stream has been[/color]
      *closed*. It[color=blue]
      >should block if there's just no data ready.
      >[color=green]
      >> Can anybody post some example code that shows how to[/color][/color]
      read[color=blue][color=green]
      >> the entire contents of a network stream and handling[/color][/color]
      if[color=blue][color=green]
      >> there isn't anything in it?[/color]
      >
      >Your protocol should indicate when all the data has been[/color]
      read, either[color=blue]
      >by stating in advance how much there is to read, or by[/color]
      indicating the[color=blue]
      >"end of data". Alternatively, if the client isn't[/color]
      expected to do[color=blue]
      >anything else afterwards, the server can close the[/color]
      socket.[color=blue]
      >
      >--
      >Jon Skeet - <skeet@pobox.co m>
      >http://www.pobox.com/~skeet
      >If replying to the group, please do not mail me too
      >.
      >[/color]

      Comment

      • Kevin Aubuchon

        #4
        Re: ReadLine from a network stream hangs if it is empty

        Use the DataAvailable property on NetworkStream.

        kevin aubuchon


        "Scott" <Scott@discussi ons.microsoft.c om> wrote in message
        news:3d4401c472 97$419d1be0$a50 1280a@phx.gbl.. .[color=blue]
        > Hi Jon,
        >
        > Thanks for the info, I guess my real problem is that I
        > don't know what the correct method is to check if there
        > is any data in the stream without causing it to screw up
        > (if there is no data ready)
        >
        > Is there a way to check if readline has anything in it
        > before trying to get data from it? because currently if
        > we try and get the contents of readline() into a variable
        > and readline is empty, it exits our 'never ending' loop
        > and processing stops completely, but we dont get any
        > errors thrown at us
        >
        > Cheers,
        >
        > Scott
        >
        >[color=green]
        > >-----Original Message-----
        > >Scott <scott@discussi ons.microsoft.c om> wrote:[color=darkred]
        > >> If I try to call read(), readline(), readtoend() and
        > >> there is nothing to read (from a never ending loop for
        > >> example) the program seems to continue but it exits[/color][/color]
        > the[color=green][color=darkred]
        > >> loop for no apparent reason.
        > >>
        > >> We also can't check the stream for the length, as the
        > >> network stream doesn't support seek operations
        > >>
        > >> MSDN reckons that the functions should return null if
        > >> there is nothing to read but it doesn't.[/color]
        > >
        > >No, ReadLine should return null if the stream has been[/color]
        > *closed*. It[color=green]
        > >should block if there's just no data ready.
        > >[color=darkred]
        > >> Can anybody post some example code that shows how to[/color][/color]
        > read[color=green][color=darkred]
        > >> the entire contents of a network stream and handling[/color][/color]
        > if[color=green][color=darkred]
        > >> there isn't anything in it?[/color]
        > >
        > >Your protocol should indicate when all the data has been[/color]
        > read, either[color=green]
        > >by stating in advance how much there is to read, or by[/color]
        > indicating the[color=green]
        > >"end of data". Alternatively, if the client isn't[/color]
        > expected to do[color=green]
        > >anything else afterwards, the server can close the[/color]
        > socket.[color=green]
        > >
        > >--
        > >Jon Skeet - <skeet@pobox.co m>
        > >http://www.pobox.com/~skeet
        > >If replying to the group, please do not mail me too
        > >.
        > >[/color][/color]


        Comment

        • Jon Skeet [C# MVP]

          #5
          Re: ReadLine from a network stream hangs if it is empty

          Scott <Scott@discussi ons.microsoft.c om> wrote:[color=blue]
          > Thanks for the info, I guess my real problem is that I
          > don't know what the correct method is to check if there
          > is any data in the stream without causing it to screw up
          > (if there is no data ready)
          >
          > Is there a way to check if readline has anything in it
          > before trying to get data from it? because currently if
          > we try and get the contents of readline() into a variable
          > and readline is empty, it exits our 'never ending' loop
          > and processing stops completely, but we dont get any
          > errors thrown at us[/color]

          As Kevin says, you can use DataAvailable - but then you can easily read
          to the point where that will return false even though there's more data
          which is *going* to be sent. As I say, the *protocol* should indicate
          whether or not there should be any data in the stream.

          --
          Jon Skeet - <skeet@pobox.co m>
          Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

          If replying to the group, please do not mail me too

          Comment

          • Scrubbing Bubbles

            #6
            Re: ReadLine from a network stream hangs if it is empty



            while(true)
            {

            int bytes = cSocket.Receive (buffer, buffer.Length, 0);
            mes += ASCII.GetString (buffer, 0, bytes);

            if(bytes < buffer.Length)
            {
            break;
            }
            }


            Scott wrote:
            [color=blue]
            > Hi Jon,
            >
            > Thanks for the info, I guess my real problem is that I
            > don't know what the correct method is to check if there
            > is any data in the stream without causing it to screw up
            > (if there is no data ready)
            >
            > Is there a way to check if readline has anything in it
            > before trying to get data from it? because currently if
            > we try and get the contents of readline() into a variable
            > and readline is empty, it exits our 'never ending' loop
            > and processing stops completely, but we dont get any
            > errors thrown at us
            >
            > Cheers,
            >
            > Scott
            >
            >[color=green]
            >>-----Original Message-----
            >>Scott <scott@discussi ons.microsoft.c om> wrote:[color=darkred]
            >>> If I try to call read(), readline(), readtoend() and
            >>> there is nothing to read (from a never ending loop for
            >>> example) the program seems to continue but it exits[/color][/color]
            > the[color=green][color=darkred]
            >>> loop for no apparent reason.
            >>>
            >>> We also can't check the stream for the length, as the
            >>> network stream doesn't support seek operations
            >>>
            >>> MSDN reckons that the functions should return null if
            >>> there is nothing to read but it doesn't.[/color]
            >>
            >>No, ReadLine should return null if the stream has been[/color]
            > *closed*. It[color=green]
            >>should block if there's just no data ready.
            >>[color=darkred]
            >>> Can anybody post some example code that shows how to[/color][/color]
            > read[color=green][color=darkred]
            >>> the entire contents of a network stream and handling[/color][/color]
            > if[color=green][color=darkred]
            >>> there isn't anything in it?[/color]
            >>
            >>Your protocol should indicate when all the data has been[/color]
            > read, either[color=green]
            >>by stating in advance how much there is to read, or by[/color]
            > indicating the[color=green]
            >>"end of data". Alternatively, if the client isn't[/color]
            > expected to do[color=green]
            >>anything else afterwards, the server can close the[/color]
            > socket.[color=green]
            >>
            >>--
            >>Jon Skeet - <skeet@pobox.co m>
            >>http://www.pobox.com/~skeet
            >>If replying to the group, please do not mail me too
            >>.
            >>[/color][/color]

            Comment

            • Jon Skeet [C# MVP]

              #7
              Re: ReadLine from a network stream hangs if it is empty

              Scrubbing Bubbles <scrubbling.bub bles@scrubbing. bubbles> wrote:[color=blue]
              > while(true)
              > {
              >
              > int bytes = cSocket.Receive (buffer, buffer.Length, 0);
              > mes += ASCII.GetString (buffer, 0, bytes);
              >
              > if(bytes < buffer.Length)
              > {
              > break;
              > }
              > }[/color]

              No, that's no good - there's no guarantee that a network stream will
              return data filling the whole buffer even if there's then going to be
              data available in a moment.

              --
              Jon Skeet - <skeet@pobox.co m>
              Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

              If replying to the group, please do not mail me too

              Comment

              • Jell-O Biafra

                #8
                Re: ReadLine from a network stream hangs if it is empty


                How about this?

                // Address of URL
                string URL = textBox1.Text;
                try
                {
                // Get HTML data
                WebClient client = new WebClient();
                Stream data = client.OpenRead (URL);
                StreamReader reader = new StreamReader(da ta);
                string str = "";
                str = reader.ReadLine ();


                while( str != null)
                {
                Console.WriteLi ne(str);
                str = reader.ReadLine ();
                }
                data.Close();
                }
                catch(WebExcept ion exp)
                {
                MessageBox.Show (exp.Message, "Exception" );
                }


                Jon Skeet [C# MVP] wrote:
                [color=blue]
                > Scrubbing Bubbles <scrubbling.bub bles@scrubbing. bubbles> wrote:[color=green]
                >> while(true)
                >> {
                >>
                >> int bytes = cSocket.Receive (buffer, buffer.Length, 0);
                >> mes += ASCII.GetString (buffer, 0, bytes);
                >>
                >> if(bytes < buffer.Length)
                >> {
                >> break;
                >> }
                >> }[/color]
                >
                > No, that's no good - there's no guarantee that a network stream will
                > return data filling the whole buffer even if there's then going to be
                > data available in a moment.
                >[/color]

                Comment

                • Jon Skeet [C# MVP]

                  #9
                  Re: ReadLine from a network stream hangs if it is empty

                  Jell-O Biafra <jbiafra@dead.k ennedies> wrote:[color=blue]
                  > How about this?
                  >
                  > // Address of URL
                  > string URL = textBox1.Text;
                  > try
                  > {
                  > // Get HTML data
                  > WebClient client = new WebClient();
                  > Stream data = client.OpenRead (URL);
                  > StreamReader reader = new StreamReader(da ta);
                  > string str = "";
                  > str = reader.ReadLine ();
                  >
                  >
                  > while( str != null)
                  > {
                  > Console.WriteLi ne(str);
                  > str = reader.ReadLine ();
                  > }
                  > data.Close();
                  > }
                  > catch(WebExcept ion exp)
                  > {
                  > MessageBox.Show (exp.Message, "Exception" );
                  > }[/color]

                  Well, I'd use:

                  string str;

                  while ( (str=reader.Rea dLine()) != null)
                  {
                  ...
                  }

                  myself, but yes, that's the gist of it. (I'd also use using statements
                  to close the reader/stream, btw.)

                  However, that then gets back to the OP's problem of ReadLine blocking
                  when there's no more data available but the stream hasn't been closed
                  by the other end.

                  --
                  Jon Skeet - <skeet@pobox.co m>
                  Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

                  If replying to the group, please do not mail me too

                  Comment

                  Working...