Solved

Delphi UDP broadcast / multicast issues

Posted on 2010-11-28
7
2,659 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Configuring network clients can be a chore, especially if there are a large number of them or a lot of itinerant users.  DHCP dynamically manages this process, much to the relief of users and administrators alike!
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

912 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

17 Experts available now in Live!

Get 1:1 Help Now