Advice and or examples for broadcasting data on a LAN to multiple destinations lazarus / 21-General ---------------------------------------------------------------------- andyf97 · 2023-06-13 04:07 So chaps, I have 200 Raspberry pies sitting on a Local LAN, each with a fixed IP address 192.168.0.X, where X is a consecutive range starting with 2 I want to send a bunch of bytes to all of those pies. instructions intended to get the pies doing a task approximately at the same time. Like to send a GO to them. Is this possible and what would be the way to go with Lazarus ideally without third party extensions. Can all pies receive data at the same time. ---------------------------------------------------------------------- dbannon · 2023-06-13 05:09 What you are  taking about is called "multicast", was popular in some of the early video conferencing models but network engineers hated it. But within one subnets, should be possible. I have used it but have no idea what happens under the  surface other than the apps at all ends must be doing multicast. Perhaps googling for 'multicast' would be a good start. Davo ---------------------------------------------------------------------- Zvoni · 2023-06-13 05:57 My first try would be Sockets and/or one of the network-libs synapse, indy, lnet ---------------------------------------------------------------------- Warfley · 2023-06-13 09:38 You basically have two options, either you use UDP Broadcast, this will send a message to all devices in the network. Otherwise you connect to each device individually and send a message to them. For the UDP broadcast, every network has a broadcast address which ends in 255. You can just send an UDP to that address and everyone listening on the network should receive it. Here is a simple example using the sockets unit: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i program broadcast_example; {$mode objfpc}{$H+}{$Assertions on} uses  cthreads, SysUtils, Classes, Sockets;  procedure Broadcaster;var  Sock: Tsocket;  Addr: TInetSockAddr;  Msg: String = 'Hello Broadcast';  OptVal, SetOptResult: Integer;  SendLen: SizeInt;begin  // Wait for receiver to be setup  Sleep(1000);   Sock := fpsocket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);  Assert(Sock > 0, 'Error Creating Socket');  OptVal := 1;  SetOptResult := fpsetsockopt(Sock, SOL_SOCKET, SO_BROADCAST, @OptVal, SizeOf(OptVal));  Assert(SetOptResult = 0, 'Error Setting Broadcast');   WriteLn('Sending BroadCast: "', Msg, '"');  // Send Broadcast  Addr.sin_family := AF_INET; // IPv4 Addr  Addr.sin_addr := StrToNetAddr('127.255.255.255'); // Broadcast addr  Addr.sin_port := htons(1337); // Port  SendLen := fpsendto(Sock, @Msg[1], Msg.Length, 0, @Addr, SizeOf(Addr));  Assert(SendLen = Msg.Length, 'Error Sending Message');end; procedure Receiver;var  Sock: Tsocket;  ListenAddr: TInetSockAddr;  Msg: String;  SenderAddrLen: tsocklen;  SenderAddr: TInetSockAddr;  MessageLen: SizeInt;begin  Sock := fpsocket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);  Assert(Sock > 0, 'Error Creating Socket');         ListenAddr.sin_family := AF_INET; // IPv4 Addr  ListenAddr.sin_addr := StrToNetAddr('0.0.0.0'); // Receive over any NIC  ListenAddr.sin_port := htons(1337); // Listening Port  fpbind(Sock, @ListenAddr, SizeOf(ListenAddr));   SetLength(Msg, 512); // Max UDP Datagram length  SenderAddrLen := SizeOf(SenderAddr);  MessageLen := fprecvfrom(Sock, @Msg[1], Length(Msg), 0, @SenderAddr, @SenderAddrLen);  SetLength(Msg, MessageLen);  WriteLn('Received Message [', HostAddrToStr(SenderAddr.sin_addr), ':',          NToHs(SenderAddr.sin_port), ']: ', Msg);end; var  BCThread: TThread;  RCThread: TThread;begin  // As I don't have two PCs at hand, I will simply use two threads and the loopback address  BCThread := TThread.CreateAnonymousThread(@Broadcaster);  RCThread := TThread.CreateAnonymousThread(@Receiver);  BCThread.FreeOnTerminate := False;  RCThread.FreeOnTerminate := False;  BCThread.Start;  RCThread.Start;  BCThread.WaitFor;  BCThread.Free;  RCThread.WaitFor;  RCThread.Free;end.  A few notes: Not every network supports broadcasting, e.g. the Internet doesn't (i.e. you can't just send a message to 255.255.255.255 and expect it be sent to all devices on the internet). Check your router if it doesn't work. In the example above I have used the broadcast address of the loopback address, because I tested it locally (and broadcast will not be received by the PC that send it). For you the broadcasting address will be either 192.168.0.255 or 192.168.255.255 depending if you have a 24 or 16 bit subnet (if anything in between it's of course different). Lastly, for simplicety I have bound the receiver to 0.0.0.0, which means it can receive messages from any network interface, meaning it would receive messages from the loopback (as in this example), from the cable network or from the wireless network, as those are the 3 network interfaces my computer currently has. If your devices have multiple network interfaces, you may want to restrict this to a specific IP address on one interface, to not get any "surprise" messages. Also a note of caution, while the code above is written to be OS agnostic, i.e. run on windows and on Linux, Windows has this really annoying property of not liking pointers to the stack for some datastructures, see https://stackoverflow.com/questions/861154/winsock-error-code-10014 So when you encounter an 10014 error, you may need to put the addr data on the heap (using pointers and new/dispose)