oddtwelve
oddtwelve

Reputation: 1036

Receiving continuous data stream with sockets in C#

I'm trying to write a client application, which connects to a server on a specified port and receives data.

I found this fine example, but my problem is that the server is sending me data all the time (not closing the conection), and so ReceiveCallback never ends, because client.EndReceive(ar) never returns 0.

So, my WinForm is freezing during receiving data.

The idea is to monitor all the incoming data and make some callbacks on certain occasions.

I'm new to C#, could you point me to right direction? Multithreading?

Upvotes: 0

Views: 1544

Answers (1)

John Källén
John Källén

Reputation: 7943

Have two threads: one for the user interface, and another that reads from the "infinite socket". The infinite thread loops forever reading from the socket in appropriately sized chunks, perhaps doing some preprocessing. It then uses Control.Invoke() to call a method on the UI thread, passing the chunk to it in a parameter. Make sure the chunks are small enough that the UI thread can process them without locking up, say in 0.1 secs.

Upvotes: 1

Related Questions