views:

1582

answers:

1

At the first look when using asynchronous methods on the socket level, there shouldn't be problems with sending and receiving data from the associated network stream, or 'directly' on the socket. But, you already probably know, there is.

The problem is particularly highlighted on the Windows Mobile platform and the Compact Framework.

I use asynchronous methods, BeginReceive and the callback function which performs ends a pending asynchronous read for the received data (EndReceive) from the async. result.

As i need to constantly receive data from the socket, there is a loop which waits for the data. The problems begins when i want to send data. For that purposes before sending some data through the socket i'm "forcing" ends of asynchronous read with EndReceive. There is a great delay on that (sometimes you just can't wait for this timeout). Timeout is too long, and i need to immediately send the data. How? I don't want to close the socket and reconnect.

I use synchronous method Send for sending data (although the results are the same with async. methods BeginSend/EndSend). When sending is finished, i start to receive data again.

Resources that i know about:

P.S.:I tried to send the data without ending asynchronous receive but then i got SocketException: A blocking operation is currently executing (ErrorCode: 10036).

Thanks in advance! I hope that i'm not alone in this 'one little problem'.. : )

+1  A: 

Have you considered to use Poll (or Select for multiple sockets) static method instead of BeginReceive to check if there are data for read? In my opinion this is causing you the trouble.

Yakeen
Yes, indeed, using Poll method i can always read and write through socket without blocking, errors, exceptions (so far) etc.Thank you very, very much! I wasted 2 days in searching for this answer. :)Reference: http://msdn.microsoft.com/en-us/library/system.net.sockets.socket.poll.aspx
beaver