UDP using broadcast doesn't work [SOLVED]

I’ve seen a lot in regards to UDP issues but I couldn’t find anything in regards to the way I am trying to use it.

I want to send a UDP to a broadcast address, eg, 192.168.1.255 to port 5003 so that any listening device on the network can receive this. I then have an application running on the PC listening to that port.

The issue I have is that when I call udp.send(msg); it blocks and never returns. This is the code I am using. Cut and pasted the parts that are relevant.

char msg[64];
    
sprintf(msg, "%s,%s", event, data);
    
udp.beginPacket(haServer, serverPort);
udp.write(msg);
udp.endPacket();

I have a udp.begin(serverPort); in setup.

serverPort is set for 5003 and haServer is set for 192.168.1.255

I even tried adding a delay after the endPacket call but still never returns.

1 Like

Hi @v8dave

I think your problem is

udp.write(msg);

which should be

udp.write(msg,strlen(msg));

I believe you are doing a one byte write, not a multibyte write.

1 Like

Thanks bko. The issue actually turned out to be that I was trying to send on the same port I was listening to.

I used 5004 for udp.begin(5004); and then used 5003 in udp.beginPacket(haServer, serverPort); and it works now. It’s been running all afternoon up until now without missing a beat.

Seems that if you use the same port, your receive back your own message. ??

Yes, with a broadcast address, you get your own packets back on rx. With a non-broadcast address, obviously it works in the normal way.

Solved then?

I’d say yes, I can work around the issue and have a working system. It would be nice just to be able to send without having to create a listener but this for now works fine on this network…

1 Like