在多个线程中从同一个 UDP 套接字发送 [英] Sending from the same UDP socket in multiple threads

查看:15
本文介绍了在多个线程中从同一个 UDP 套接字发送的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有多个线程需要将 UDP 数据包发送到不同的 IP 地址(只发送,不需要接收任何内容).我可以在所有线程中重用同一个 UDP 套接字吗?

I have multiple threads which need to send UDP packets to different IP addresses (only to send, nothing needs to be received). Can I reuse the same UDP socket in all the threads?

推荐答案

是的,我想你可以.

由于数据包是单独发出的,虽然它们的接收顺序是不确定的,但它已经是 UDP.

As the packets are sent out individually, although the order they are received will be nondeterministic, it is already with UDP.

所以在同一个套接字中发送多个线程就可以了.

So sending in multiple threads in the same socket is fine.

不过,如果您正在对套接字执行其他操作,例如 bind()、close(),那么您最终可能会遇到竞争条件,因此您可能需要小心.

Although, if you're doing other stuff with the socket, such as bind(), close(), then you could end up with race conditions, so you might want to be careful.

这篇关于在多个线程中从同一个 UDP 套接字发送的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆