Solved

Delphi UDP broadcast / multicast issues

Posted on 2010-11-28
7
2,641 Views
Last Modified: 2012-05-10
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;
var
  SocketAddressInfo: TSockAddrIn;
  AddressInfoSize: integer;
begin
  Result := 0;

  if SocketIsBound then
  begin
    if Size <= 512 then
    begin
      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)
      else
        Result := SendTo(SocketHandle, Buffer, Size, 0, SocketAddressInfo, AddressInfoSize);

      if Result < 0 then
      begin
        OnError('Broadcast error: ' + WinSockError(Result));
        Result := 0;
      end;
    end
    else
      OnError('Broadcast error: Packet too large (Maximum 512 bytes)');
  end
  else
    OnError('Broadcast error: Socket not bound');
end;
0
Comment
Question by:aburgoyne
  • 4
  • 3
7 Comments
 

Author Comment

by:aburgoyne
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?
0
 
LVL 13

Expert Comment

by:ThievingSix
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.
0
 

Author Comment

by:aburgoyne
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.
0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 13

Expert Comment

by:ThievingSix
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?
0
 

Author Comment

by:aburgoyne
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. :-(
0
 
LVL 13

Accepted Solution

by:
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?  
0
 

Author Comment

by:aburgoyne
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 :-(
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

708 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now