I have a UDP Server listening on my local network on IP:192.168.0.53 port 1337.
I have a UDP client also on the same subnet of my local network setup to send a packet to 255.255.255.255 port 1337 and it never arrives on my server. This happens repeatedly, and I am using the broadcast address as you can see.
I try from my client to send a packet to 192.168.0.53 port 1337 and it arrives fine. Showing a simpler non broadcast route works nicely.
I have tried 2 clients, as I believe some special flags need to be set for broadcast. Both PacketSender and this specific broadcast application are not received by my client.
My server is written using the ESP8266WiFi UDP class.
Am I missing something here? The broadcast should be received by my server but isn't. Do I need a special flag on my server perhaps?
The server code (based off of the Udp class linked above) simply is:
int packetSize = Udp.parsePacket();
if (packetSize){
// receive incoming UDP packets
String msg = "Received ";
msg += String(packetSize)+" bytes from IP:";
msg += Udp.remoteIP().toString()+" on port:"+Udp.remotePort();
this->DCPrintf(msg);
int len = Udp.read(incomingPacket, 255);
if (len > 0)
{
incomingPacket[len] = 0;
}
msg = "UDP packet contents:";
msg +=incomingPacket;
this->DCPrintf(msg);
}
Sending code from this github https://github.com/stanwu/udp-broadcast:
/* fpont 12/99 */
/* pont.net */
/* udpClient.c */
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h> /* memset() */
#include <sys/time.h> /* select() */
//for Mac OS X
#include <stdlib.h>
#define REMOTE_SERVER_PORT 1500
#define MAX_MSG 100
int main(int argc, char *argv[]) {
int sd, rc, i;
struct sockaddr_in cliAddr, remoteServAddr;
struct hostent *h;
int broadcast = 1;
/* check command line args */
if(argc<3) {
printf("usage : %s <server> <data1> ... <dataN> \n", argv[0]);
exit(1);
}
/* get server IP address (no check if input is IP address or DNS name */
h = gethostbyname(argv[1]);
if(h==NULL) {
printf("%s: unknown host '%s' \n", argv[0], argv[1]);
exit(1);
}
printf("%s: sending data to '%s' (IP : %s) \n", argv[0], h->h_name,
inet_ntoa(*(struct in_addr *)h->h_addr_list[0]));
remoteServAddr.sin_family = h->h_addrtype;
memcpy((char *) &remoteServAddr.sin_addr.s_addr,
h->h_addr_list[0], h->h_length);
remoteServAddr.sin_port = htons(REMOTE_SERVER_PORT);
/* socket creation */
sd = socket(AF_INET,SOCK_DGRAM,0);
if(sd<0) {
printf("%s: cannot open socket \n",argv[0]);
exit(1);
}
if (setsockopt(sd, SOL_SOCKET, SO_BROADCAST, &broadcast,sizeof broadcast) == -1) {
perror("setsockopt (SO_BROADCAST)");
exit(1);
}
/* bind any port */
cliAddr.sin_family = AF_INET;
cliAddr.sin_addr.s_addr = htonl(INADDR_ANY);
cliAddr.sin_port = htons(0);
rc = bind(sd, (struct sockaddr *) &cliAddr, sizeof(cliAddr));
if(rc<0) {
printf("%s: cannot bind port\n", argv[0]);
exit(1);
}
/* send data */
for(i=2;i<argc;i++) {
rc = sendto(sd, argv[i], strlen(argv[i])+1, 0,
(struct sockaddr *) &remoteServAddr,
sizeof(remoteServAddr));
if(rc<0) {
printf("%s: cannot send data %d \n",argv[0],i-1);
close(sd);
exit(1);
}
}
return 1;
}
And the other client is simply the PacketSender software.
The solution was to use multicast instead.
My server code was changed to join a multicast group and listen on a particular port:
int port=12345;
IPAddress local=WiFi.localIP();
IPAddress multicastGroup(233, 100, 100, 53);
Udp.beginMulticast(local,multicastGroup,port);
Then I from any client sent a UDP packet to IP 233.100.100.53
port 12345
, which is the multicast group, and voila -- it works a treat!