lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Everyone,

Thanks for all the suggestions and help. I think I've solved my problem. The magic came with setting to DONTROUTE option on the socket. The following seems to get me where I need to go:

udp = socket.udp()
print(udp:setoption('broadcast',true))
print(udp:setoption('dontroute',true))
print(udp:setsockname('10.0.1.130',47808))
print(udp:sendto('this is a test', '10.0.1.255', 47808))

The next question is to send it a stream of non-character bytes. Anyone have any suggestions on that?

Thanks,
Snowdall

On Nov 14, 2009, at 11:44 AM, Sean Conner wrote:

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


Attachment: smime.p7s
Description: S/MIME cryptographic signature