简体   繁体   中英

C# TCP socket, asynchronous read and write at the “same” time (.NET CF), how?

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 platform and the . 平台和上特别突出了该问题。

I use , BeginReceive and the callback function which performs ends a pending asynchronous read for the received data (EndReceive) from the async. ,BeginReceive和回调函数来结束从异步接收的数据(EndReceive)的挂起异步读取。 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 on that (sometimes you just can't wait for this ). (有时您不能等待此 )。 Timeout is too long, and . 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:

PS:I tried to send the data without ending asynchronous receive but then i got (ErrorCode: 10036). (错误代码:10036)。

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM