How to send multicast messages and reuse a port in Erlang?

99封情书 提交于 2019-12-03 13:55:06

UPDATED: ok, I have found what I believe to be a working solution. The crucial point it seems relate to joining a multicast group.

{ok, Socket} = gen_udp:open(Port=5353, [binary, {active, false}, {reuseaddr, true},
                                        {ip, Addr}, {add_membership, {Addr, IAddr}}]).
  1. Addr: multicast group (e.g. {224, 0, 0, 251}
  2. IAddr is a local IP interface (e.g. can use default {0,0,0,0})

( Of course, make sure that you are not running DNS daemon that might enter in conflict)

don't have enough rep to reply to the {broadcast,true} discussion under emil's post, sorry.

The SO_BROADCAST socket flag (which I assume that maps to) must be set or sendto(a broadcast address) will fail. This is a safety catch to prevent abuse or errors with programs that didn't intend to broadcast. Otherwise secure programs would have to try to check for broadcast addresses themselves.

enabling SO_BROADCAST doesn't stop you from sending non-broadcast packets. (again, assuming erlang's stuff just maps directly to setsockopts; I don't know erlang, just networking!)

You might want to try strace to see what system calls actually happen. look for socket(), and then what happens to that file descriptor.

You try to open a socket which is already open? Can't you use the same socket for sending and receiving?

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!