Search code examples
delphiudpiplan

Send message to all connected machines using Delphi using UDP or other low level technique?


I am making a game and want to talk to all the clients at once from my game server. (Original question: How to make a list of IPs, updated question: how to do a UDP broadcast.)

And i want to make a LAN connections between the players. Now i dont understand how to find all connected machines to send them a message with server information. Or how to send a UDP to all with Synapse?


Solution

  • It isn't easily possible to enumerate a complete list of IP addresses on your LAN, at least not on a raw TCPIP level, which is what you originally asked for (a list of IP addresses). Then you updated the question to ask about sending a message to all connected machines. There is a comment above that recommends using NETSTAT info to do this, but I am not sure that would be a reliable solution for what you want to do.

    Reason #1 why it's difficult to enumerate IPs is that many attached computers won't respond at all to anything, including Ping. Second problem is you would have to broadcast out to all local addresses, if you wanted to enumerate computers in the current segment, and this would take a long time, and would not be complete, because it is possible that your local area network segment could have multiple sets of IP addresses, perhaps on several different private-ip subnets, and one or more public-ip subnets.

    If you're making a game, and you wrote a TCP based game server, your game server knows who is connected to the server currently, and you should be able to go through the active sessions. The code to do that would be different for Indy, ICS, Synapse, or whatever other library you're using to write your TCP/IP or UDP Game Protocol Server code. You could use UDP broadcast message but that might not be the right solution for some game systems. Imagine you're running two servers on the same computer, and some players are connected to each one. A connection-oriented solution is more robust. Your clients might still use UDP and might poll the server they are connected to, and receive notification of events by flags in your protocol. That would be far more robust than attempting to multicast or broadcast to a LAN.

    However, Synapse does support a broadcast syntax if you insist:

     socket := TUDPBlockSocket.create;
     socket.enablebroadcast(true);
     socket.Connect('255.255.255.255', '12345')
     socket.SendBufferTo(@DATARECORD, SIZEOF(DATARECORD));