Delphi UDP broadcast / multicast issues
Posted on 2010-11-28
I've tweaked a Delphi UDP class that I found on Torry.net and it basically does everything I need.
I need to be able to send messages simultaneously to large numbers of "client" systems and have successfully opened and sent messages to 45,000 client instances on a single Win 2003 server by using 3 RDP sessions (one of which also contained the "server" application).
The only issue I'm batting my head against is being about to broadcst messages to remote clients as the system seems intent on using 172.16.138.1 as the broadcasting IP address (which is in the APIPA range and therefore non-routeable) regardless of whether I specify Local broadcasting or not.
This is the source of the broadcasting function within the class that I'm using:
function EUDPSocket.Broadcast(Port: word; var Buffer; Size: integer; Local: boolean): integer;
Result := 0;
if SocketIsBound then
if Size <= 512 then
FillChar(SocketAddressInfo, SizeOf(SocketAddressInfo), 0);
SocketAddressInfo.sin_family := AF_INET;
SocketAddressInfo.sin_port := htons(Port);
SocketAddressInfo.sin_addr.S_addr := integer(INADDR_BROADCAST);
AddressInfoSize := SizeOf(SocketAddressInfo);
if Local then
Result := SendTo(SocketHandle, Buffer, Size, MSG_DONTROUTE, SocketAddressInfo, AddressInfoSize)
Result := SendTo(SocketHandle, Buffer, Size, 0, SocketAddressInfo, AddressInfoSize);
if Result < 0 then
OnError('Broadcast error: ' + WinSockError(Result));
Result := 0;
OnError('Broadcast error: Packet too large (Maximum 512 bytes)');
OnError('Broadcast error: Socket not bound');