通过同一套接字的UDP广播和单播? [英] UDP broadcast and unicast through the same socket?
问题描述
我有一个Linux应用程序,该应用程序打开UDP套接字并将其绑定到端口。通过套接字发送单播数据包,我没有任何问题。我本来有机会发送广播数据包,所以我启用了SO_BROADCAST,它允许广播数据包通过,但是后来我注意到单播数据包也正在广播。这是UDP套接字的预期行为,还是我配置错误的可能性更大?
I have a Linux application that opens a UDP socket and binds it to a port. I haven't had any problem sending unicast packets through the socket. I had occasion to send a broadcast packet, so I enabled SO_BROADCAST, which allowed the broadcast packets to pass, but then I noticed that the unicast packets were being broadcast as well. Is this expected behaviour for a UDP socket, or is it more likely that I've misconfigured something?
推荐答案
据我了解 SO_BROADCAST 是一个套接字选项。因此,如果您在套接字上启用它,则此套接字将广播。我想如果要从同一代码进行单播和广播,则需要打开不同的套接字。
From what I understand SO_BROADCAST is a socket option. So if you enable it on your socket this socket will broadcast. I guess you will need to open different sockets if you want to do unicast and broadcast from the same code.
这篇关于通过同一套接字的UDP广播和单播?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!