How to use data receive event in Socket class?

Posted by affan on Stack Overflow See other posts from Stack Overflow or by affan
Published on 2011-01-05T09:49:43Z Indexed on 2011/01/05 9:53 UTC
Read the original article Hit count: 249

Filed under:
|
|
|

I have wrote a simple client that use TcpClient in dotnet to communicate. In order to wait for data messages from server i use a Read() thread that use blocking Read() call on socket. When i receive something i have to generate various events. These event occur in the worker thread and thus you cannot update a UI from it directly. Invoke() can be use but for end developer its difficult as my SDK would be use by users who may not use UI at all or use Presentation Framework. Presentation framework have different way of handling this. Invoke() on our test app as Microstation Addin take a lot of time at the moment. Microstation is single threaded application and call invoke on its thread is not good as it is always busy doing drawing and other stuff message take too long to process.

I want my events to generate in same thread as UI so user donot have to go through the Dispatcher or Invoke.

Now i want to know how can i be notified by socket when data arrive? Is there a build in callback for that. I like winsock style receive event without use of separate read thread. I also do not want to use window timer to for polling for data.

I found IOControlCode.AsyncIO flag in IOControl() function which help says

Enable notification for when data is waiting to be received. This value is equal to the Winsock 2 FIOASYNC constant.

I could not found any example on how to use it to get notification. If i am write in MFC/Winsock we have to create a window of size(0,0) which was just used for listening for the data receive event or other socket events. But i don't know how to do that in dotnet application.

© Stack Overflow or respective owner

Related posts about c#

Related posts about .NET