从多个线程中的同一UDP套接字发送

从多个线程中的同一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.