UDP Broadcast (was Re: Program Linking)

Dar Scott dsc at swcp.com
Wed Mar 13 17:03:01 EST 2002


On Wednesday, March 13, 2002, at 01:48 PM, Richard Gaskin wrote:
> It would be more practical, as some have suggested here, to use UDP
> broadcasting instead of walking through the subnet list.  But I've had
> difficulty getting UDP broadcasting to work in Rev.  Anyone have 
> an example
> of how to do a UDP broadcast in Transcript?

This works on W2K and OS X:

If you can send UDP, then you can UDP broadcast.  Use a destination 
address of 255.255.255.255.

This is normally translated to a subnet broadcast address. For 
example, for the network 10.5.5.0 with subnet mask 255.255.255.0, 
the actual address used will be 10.5.5.255.  The consequence is 
that if you have more than one subnet on the LAN, only those on the 
same subnet as the sender will get the message.  Almost everybody 
who uses Ethernet will use only one subnet on the same Ethernet 
collision space, though.

This script will illustrate:

local sendSocket
on mouseUp
   put field "255.255.255.255:" & field "Send Port" into sendSocket
   open datagram socket to sendSocket
   write field "Send Data" to socket sendSocket
   -- write more if you need to; each will be one datagram
   close socket sendSocket
end mouseUp

I assume multicasts will work also.


Ah, but receiving.  There's the rub.

I use "accept datagram socket".  There might be a better way.  I 
described how I do this in recent mail to improve-revolution with 
the subject ""accept datagram" on W2K is not that on OS X".

Essentially, I open with "accept" specifying the message for 
handling datagrams.  Those messages are sent with the sender's IP 
address and with the datagram content.  NB: Close the socket with 
the port only; if you look at openSocket, you will see the socket 
listed in that form.

The problem (or feature) with OS X is that an additional message is 
sent upon "accept" and only one valid datagram can be received.  
The "datagram" for the extra message empty, so if you make sure no 
valid datagram is empty, you can ignore it based on that for both 
platforms.  If you need to receive more than one datagram, you have 
some work to do.  Otherwise just close it at the end of the second 
message handling.  You can reopen it to receive more.

Anybody know how this works on other platforms?

Notice that the sending and receiving are completely independent 
sockets.

I haven't figured out a way for either the sender or receiver to 
get the sender's port, so, for now, protocols will have to ignore 
that.

This will receive broadcasts from the local computer as well as 
others.  That is, you will receive your own broadcasts.  To tell 
those apart you will need to either find out your own IP address or 
compare against your own hash code (from milliseconds()?) in your 
datagrams.


I have a tiny UDP example stack.  I don't know whether it is a good 
example; I'm new to Revolution.

Dar Scott




More information about the use-livecode mailing list