Delphi UDP broadcast / multicast issues

Posted on 2010-11-28
Last Modified: 2012-05-10
I've tweaked a Delphi UDP class that I found on 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 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;
  SocketAddressInfo: TSockAddrIn;
  AddressInfoSize: 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');
Question by:aburgoyne
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3

Author Comment

ID: 34227895
Ok ... having read a lot of pages about this issue, it seems that INADDR_BROADCAST is only intended to broadcast within the LAN.  Does anyone have a solution to widening the scope?
LVL 13

Expert Comment

ID: 34227982
Routers stop broadcast messages (I believe all layer 3 devices do actually).  MSG_DONTROUTE is usually ignored anyway in most cases I've seen (albeit not a lot). The only way to broadcast to remote devices is to send directly to them, which isn't really broadcasting. So you could have a list of remote IP addresses that you loop through and feed them into sin_addr.S_addr and then send the message.

Author Comment

ID: 34228082
I'd already built something like that but using TCP sockets.  Apart from the fact that I was stuggling to get more than about 15,000 connections opened when I actually need to get above 50,000.

I tried "faking it" by rejecting the incoming sockets and that did let me reach about 40,000 connections that way but traversing the connection list to send messages out was unacceptably slow once there were more than a few hundred sockets registered.

Any ideas how to improve the situation?  The test system is a quad core, 4GB Win 2003 box.
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

LVL 13

Expert Comment

ID: 34228100
When you say you were struggling to open connections do you mean, a number of connections in a short period of time, more than a number at all, or data wasn't being sent fast enough?

Author Comment

ID: 34228177
So far, I've tried a simple WinSock wrapper class and a couple of versions of Delphi socket wrappers to test a TCP solution but hit the same brick wall each time.

I've been playing with this for about a week - as far as I can tell, Win 2003 should be able to support about 65,000 concurrent connections but the best I've been able to achieve is about 15,000.

I also hit the same kinds of limits running the server and client instances on my Vista laptop. I've tried adding delays between connections, tweaking the registry, etc. but nothing really makes any great difference.

The 2nd approach was a multi-threaded TCP server which accepted incoming socket requests, noted the details of the client (which also had TCP server functionality) then closed the connection down. To broadcast, the server simply ran through the list of clients, opened a connection to each client-server in turn, pass the message then closed the connection again. It worked well from a technical perspective i.e. it easily supported large numbers of clients, but poorly in terms of performance - broadcasting to 40,000 remote clients was taking about 90 seconds.

The 3rd approach I tried was UDP - I ran a broadcast test with 45,000 connections on the same physical server and the messages were fully delivered in about 10 seconds. I'd read the comments in the source code of the UDP class regarding the broadcast feature and how it "could go a long way unless Local mode was selected" and thought I'd found the solution to my problems but, sadly, that doesn't seem to be the case. :-(
LVL 13

Accepted Solution

ThievingSix earned 500 total points
ID: 34228311
Yeh, broadcast messages don't go past routers, or else the internet might get flooded when you think about it. What happens when you try to get passed the 15,000?  

Author Comment

ID: 34229975
it complains about not being able to bind the new sockets. Origially, I started getting that error after about 5,000 connections but I found some registry hacks on the web and managed to increase things... just not enough :-(

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
CheckListBox usage 3 97
Graphics32 under Delphi 10.1 Berlin 2 179
I want to use librsync in my Delphi backup application. 3 84
Twebbrowser add css to the header 3 53
Article by: rfc1180
The Maximum Segment size (MSS) is an important consideration when troubleshooting connectivity via the Internet/Intranet. As the packets are routed via the Internet/Intranet, the packets must traverse through multiple routers in the path between two…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
In a recent question ( here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question