UdpClient同时接收和发送
我正在维护其他人的代码及其使用类UdpClient
。该代码声明一个 UdpClient
实例,并使用 UdpClient.Receive()
连续接收数据。
接收到数据后,会在另一个线程中对其进行处理,并且 UdpClient
再次调用 Receive()
。在处理数据的同时,同一个客户端正在发回响应。
问题:这是一个错误吗?我认为是这样,因为 UdpClient
不是线程安全的,所以你不能同时调用两个方法。无论如何,代码工作正常,但是......
I am maintaining other's code and its using the class UdpClient
. The code declares one instance of UdpClient
and receives data continuously using the UdpClient.Receive()
.
When data is received, it is processed in another thread and the UdpClient
calls Receive()
again. At the same time when the data is processed the same client is sending a response back.
Question: Is this a bug? I think so because UdpClient
is not thread safe so you can not call two methods at the same time. Anyways code is working fine but ...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
某些东西不是线程安全的事实并不意味着您不能通过不同的线程调用两个方法(甚至不能通过不同的线程调用一个方法),它只是意味着在设计该类时,它不是用线程设计的- 考虑到安全性,因此从您的 POV 来看,并发访问的结果是“不可预测的”。
这不是一个错误。这是一种误用。
The fact that something isn't thread-safe doesn't mean you can't call two methods via different threads (or even one method via different threads), it just means that when the class was designed it wasn't designed with thread-safety at mind, thus the results of concurrent access are "unpredictable" from your POV.
This is not a bug. This is a mis-use.