NetworkStream.DataAvailable

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Hai Ly Hoang - MT00KSTN

    #1

    NetworkStream.DataAvailable

    Hi,
    When NetworkStream.D ataAvailable flag is toogled ?
    The explanation of MSDN is so vague and confusing !

    Thanks




  • Rich Blum

    #2
    Re: NetworkStream.D ataAvailable

    " Hai Ly Hoang - MT00KSTN" <ReplyToGroup@M ail.com> wrote in message news:<uVfccQceD HA.2756@TK2MSFT NGP11.phx.gbl>. ..[color=blue]
    > Hi,
    > When NetworkStream.D ataAvailable flag is toogled ?
    > The explanation of MSDN is so vague and confusing !
    >[/color]
    The DataAvailable property will be true when data is present on
    the socket for reading. The Read() method will block until there is
    data that can be read. If you don't want your application to block on
    the Read(), you can first check the stream with the DataAvailable
    property. If it returns false, you know that the Read() method would
    block, and can avoid calling it.

    As a side benefit, the DataAvailable property will throw an
    Exception if the remote host has closed the stream. By catching that
    in a try-catch block you can determine when the remote host has closed
    the connection:

    try {
    if (ns.DataAvailab le)
    {
    int recv = ns.Read(data, 0, data.length)
    } else
    {
    Console.WriteLi ne("No data available yet");
    }
    } catch(SocketExc eption)
    {
    Console.WriteLi ne("The remote host has disconnected");
    ns.Close();
    }

    Hope this helps clear things up some.

    Rich Blum - Author
    "C# Network Programming" (Sybex)

    "Network Performance Open Source Toolkit" (Wiley)

    Comment

    Working...