To create a UDP server-client communication in Delphi FMX, you can use the TIdUDPClient and TIdUDPServer components from the IdUDPServer and IdUDPClient units.
Here is an example of how you can use these components to create a simple UDP server and client in Delphi FMX;
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
uses IdUDPServer, IdUDPClient; procedure TForm1.FormCreate(Sender TObject); var UDPServer TIdUDPServer; UDPClient TIdUDPClient; begin //Create the UDP server UDPServer = TIdUDPServer.Create(Self); UDPServer.DefaultPort = 12345; UDPServer.OnUDPRead = UDPServerUDPRead; UDPServer.Active = True; //Create the UDP client UDPClient = TIdUDPClient.Create(Self); UDPClient.Host = ‘127.0.0.1’; Set the host to the local loopback address UDPClient.Port = 12345; end; procedure TForm1.UDPServerUDPRead(AThread TIdUDPListenerThread; const AData TIdBytes; ABinding TIdSocketHandle); begin Process the received data Memo1.Lines.Add(TEncoding.UTF8.GetString(AData)); end; procedure TForm1.Button1Click(Sender TObject); var UDPClient TIdUDPClient; begin UDPClient = TIdUDPClient.Create(Self); try UDPClient.Host = ‘127.0.0.1’; Set the host to the local loopback address UDPClient.Port = 12345; UDPClient.Send(TEncoding.UTF8.GetBytes(Edit1.Text)); finally UDPClient.Free; end; end; |
In this example, the UDP server listens on port 12345 and prints received data to a TMemo component. The client sends data to the server when the user clicks a button.
Leave a Reply