Question about TcpListener and TcpClient

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I am writing a simple chat program in C# using TcpListener and TcpClient
class. In VB6, Winsock will rise an event when data arrives, but apparently
TcpListener does not have that. So how can I know when data arrives?

One solution I could think of is using Thread to continuously checking
whether there is data arrived or not. Is there a better way to accomplish
this?

Thank you in advance

Won
 
Actually, pooling on DataAvailable is not a good idea. Most of the time it
will be 0 if done in a tight loop and that is fantasic waste of cpu. There
are many other options such as using blocking reads, Select(), or async
reads.
 
I think you could check the DataAvailable property of NetworkStream inside
an infinite loop. If u want to give a delay to listening to reduce cpu
comsumption you could do a Thread.Sleep(your delay figure here).

Ab.
http://joehacker.blogspot.com
 
Thanks for the answers.

But can you give m some examples or links about blocking reads, Select(), or
async reads.

Thanks again for the help

Won Won
 
Back
Top