Solved

Delphi UDP broadcast / multicast issues

Posted on 2010-11-28
7
2,819 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
[X]
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
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
Simple, centralized multimedia control

Watch and learn to see how ATEN provided an easy and effective way for three jointly-owned pubs to control the 60 televisions located across their three venues utilizing the ATEN Control System, Modular Matrix Switch and HDBaseT extenders.

 
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
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…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Suggested Courses
Course of the Month9 days, 16 hours left to enroll

624 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