[ACCEPTED]-Best way to wait for TcpClient data to become available?-tcpclient
Absolutely! Just call Read(...)
on the stream. That 15 will block until data is available. Unless 14 you really have to use the TcpClient
directly, I'd normally 13 do as much as possible on the stream. If 12 you want to use the socket, just call Receive(byte[])
which 11 will block until data is available (or the 10 socket is closed).
Now if you don't want 9 to block, you can use Stream.BeginRead
or Socket.BeginReceive
to work asynchronously. (Or 8 ReadAsync
as of .NET 4.5.)
I personally find Available
to be 7 pretty much useless (on both streams and 6 sockets) and looping round with a sleep 5 is definitely inefficient - you don't want 4 to have to context switch the thread when 3 data hasn't come in, and you don't want 2 to have to wait for the sleep to finish 1 when data has come in.
More Related questions
We use cookies to improve the performance of the site. By staying on our site, you agree to the terms of use of cookies.