本文介绍了套接字ReceiveAsync和发送线程安全问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用以下示例我正在编写一个服务器程序:http://msdn.microsoft.com/en-us/library/system.net.sockets.socketasynceventargs.aspx

我有包含所有连接套接字的数组,这些套接字正在等待使用ReceiveAsync接收数据。但每10秒钟我需要向所有连接的插座发送几个字节。

最好的方法是什么?据我所知,没有办法使用ReceiveAsync,似乎SendAsync不会发送任何东西直到ReceiveAsync完成。我想这留给我Socket.Send,但是当ReceiveAsync还没有完成时使用它会是线程安全的吗?

提前谢谢。

解决方案

Using the following example I'm writing a server program: http://msdn.microsoft.com/en-us/library/system.net.sockets.socketasynceventargs.aspx

I have an array containing all connected sockets who are waiting to receive data with ReceiveAsync. But once every 10 seconds I need to send a few bytes to all connected sockets.

What would be the best way to do this? As far as I know there is no way to cancle the ReceiveAsync and it seems like SendAsync wont send anything untill ReceiveAsync has finished. I guess that leaves me with Socket.Send but would it be thread safe to use it while ReceiveAsync still hasn't finished?

Thanks in advance.

解决方案


这篇关于套接字ReceiveAsync和发送线程安全问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-26 11:40