简体   繁体   English

C#TCP套接字,在“相同”时间(.NET CF)进行异步读写,如何?

[英]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 Windows Mobile platform and the Compact Framework . Windows Mobile平台和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. 我使用异步方法 ,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. 为此,在通过套接字发送一些数据之前,我使用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). 我使用同步方法Send发送数据(尽管结果与异步方法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 SocketException: A blocking operation is currently executing (ErrorCode: 10036). PS:我尝试发送数据而没有结束异步接收,但随后出现SocketException: 当前正在执行阻止操作 (错误代码: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? 您是否考虑过使用轮询(或为多个套接字选择)而不是BeginReceive静态方法来检查是否有要读取的数据? In my opinion this is causing you the trouble. 我认为这给您带来了麻烦。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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