2
votes

First let me state that I'm not sure if this is an OSX issue or I'm leaving something out:

Goal I need to specify an interface adapter to use for UDP communications to avoid conflict with other software using the same port. ( Will have 2 network adapters).

Problem: Not receiving broadcast messages when interface adapter is specified with {ip,Address} option in gen_udp

But it works as expected when I don't use the {ip,Address} option. I receive the broadcast messages sent to 255.255.255.255 or 192.168.1.255 (both work).

{ok,Socket} = gen_udp:open(Port,[{broadcast, true},{reuseaddr, true}]).

Once I add the {ip,Address} option to specify which interface adapter to use it stops receiving broadcast. I can see the message via wireshark but the socket does not receive them.

{ok,Socket} = gen_udp:open(Port,[{broadcast, true},{reuseaddr, true},{ip,Address}]).

I noticed in the case where no {ip,Address} option is used, the Socket has an Address of {0,0,0,0}. via inet:sockname(Socket).

What am I missing??

1
when the socket is bound to 0.0.0.0 it will receive from any interface. Are you sure that the packets are being sent to the interface you bind when you use {ip, Address} ?Samuel Rivas
When I use wireshark with the target interface selected (interface of {ip,Address}) I can see the broadcast information packets. So I assume the packets are being sent and arriving at the interface. They appear in wireshark with a destination of 255.255.255.255 as expected, which is different from Address. So I assume they are being ignore or not processed by socket since they don't match. I see the same thing when I don't use {ip,Address} but the Socket receives the data.casillic

1 Answers

1
votes

In order to get broadcast packets, you must bind socket to a broadcast address For example if you want get broadcasts to interface lo in a general UNIX setup, you can set Address to {127,255,255,255}:

{ok, IfList} = inet:getifaddrs(),
{_, LoOpts} = proplists:lookup("lo", IfList),
{_, Address} = proplists:lookup(broadaddr, LoOpts)