casillic
casillic

Reputation: 1837

Erlang gen_udp not recieving broadcast when {ip,Address} specificed on Socket

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??

Upvotes: 2

Views: 685

Answers (1)

reith
reith

Reputation: 2088

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)

Upvotes: 1

Related Questions