[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: UDP Broadcast
- From: Sean Conner <sean@...>
- Date: Sat, 14 Nov 2009 14:44:21 -0500
It was thus said that the Great Sean Conner once stated:
> It was thus said that the Great Clark Snowdall once stated:
> > Everyone,
> >
> > Thanks for the suggestions ... some of them were helpful.
> >
> > I've done some more exploration, and I think the issue is I can't send
> > a UDP broadcast packet. I can see non-broadcast packets, but when I
> > select the ip address for broadcast, it doesn't show up. Could it be
> > something with my router? Or lua UDP doesn't really like broadcast?
> > Any suggestions?
>
> If you want to send a broadcast packet, you need to set an option on the
> socket to do so. In C (under Unix) this would be:
>
> rc = setsockopt(sock,SOL_SOCKET,SO_BROADCAST,&dummy,sizeof(dummy));
> if (rc < 0)
> {
> perror("setsockopt(SO_BROADCAST)");
> return(EXIT_FAILURE);
> }
>
> You'll need to find the equivilent under the Lua library you are using.
I checked back and I see you are setting the broadcast flag. I checked an
old project of mine (testing broadcast UDP packets) and I see that in the
server side I have:
local = "10.0.0.3:2020"; /* well, these are actually struct sockaddr */
remote = "10.0.0.255:2020"; /* but you get the idea ... */
sock = socket(AF_INET,SOCK_DGRAM,IPPROTO_UDP);
setsockopt(sock,SOL_SOCKET,SO_REUSEADDR,&dummy,sizeof(dummy));
setsockopt(sock,SOL_SOCKET,SO_BROADCAST,&dummy,sizeof(dummy));
bind(sock,&local,sizeof(local));
while(1)
{
sendto(sock,data,sizeof(data),0,&remote,sizeof(remote));
}
And oddly enough, in the client side:
local = "10.0.0.2:2020";
sock = socket(AF_INET,SOCK_DGRAM,IPPROTO_UDP);
setsockopt(sock,SOL_SOCKET,SO_REUSEADDR,&dummy,sizeof(dummy));
setsockopt(sock,SOL_SOCKET,SO_BROADCAST,&dummy,sizeof(dummy));
bind(sock,&local,sizeof(local));
while(1)
{
recvfrom(sock,&data,sizeof(data),0,&remote,&dum);
}
It looks like you need to set the recieving socket with the broadcast
option as well.
-spc