RejectedSoftware Forums

Sign up

UDP broadcast

This is probably because of a dumb error on my part, but I'm having a hard time broadcasting a UDP datagram. Here's the basic code I'm using:

auto connection = listenUDP(0, "255.255.255.255");
scope(exit) connection.close();
connection.canBroadcast = true;
connection.send([1, 2, 3]);

When I fire up GDB, I see that the Linux socket send() function is producing an errno of 89 (Destination address required). I've tried several different broadcast addresses (192.168.x.255, 0.0.0.0), and I've also tried using the connection.send(data, address) variant, which produces errno 22 (Invalid argument). I'm using the default libevent2 backend on Arch Linux x86_64.

Is there a bug in my code, or do I need to dig deeper?

Thanks!

Re: UDP broadcast

Am 18.12.2015 um 00:09 schrieb Benjamin L. Merritt:

This is probably because of a dumb error on my part, but I'm having a hard time broadcasting a UDP datagram. Here's the basic code I'm using:

auto connection = listenUDP(0, "255.255.255.255");
scope(exit) connection.close();
connection.canBroadcast = true;
connection.send([1, 2, 3]);

When I fire up GDB, I see that the Linux socket send() function is producing an errno of 89 (Destination address required). I've tried several different broadcast addresses (192.168.x.255, 0.0.0.0), and I've also tried using the connection.send(data, address) variant, which produces errno 22 (Invalid argument). I'm using the default libevent2 backend on Arch Linux x86_64.

Is there a bug in my code, or do I need to dig deeper?

Thanks!

You definitely need to use either connect, or pass a destination
address to send. The address passed to listenUDP just defines the
network interface on which UDP packets will be received. Usually this
will be "0.0.0.0" or "127.0.0.1" for IPv4. B

When you tried with connect, what did you pass as the address parameter?
The following should work, but I didn't try to run it yet:

auto connection = listenUDP(1234, "0.0.0.0");
scope (exit) connection.close();

connection.canBroadcast = true;
connection.connect("255.255.255.255", 1234);
send([1, 2, 3]);

Re: UDP broadcast

On Sat, 19 Dec 2015 13:26:53 +0100, Sönke Ludwig wrote:

You definitely need to use either connect, or pass a destination
address to send. The address passed to listenUDP just defines the
network interface on which UDP packets will be received.

Oh, that makes sense now. It seems to be working fine with connect() now. I'm not sure why send(data, address) was producing an errno 22, but for what I'm doing, connect() works better than the 2-argument send() anyway.