Delphi Udp -

// Process the message (e.g., display in a memo) TThread.Queue(nil, procedure begin Memo1.Lines.Add(Format('[%s:%d] %s', [RemoteIP, RemotePort, ReceivedString])); end); end;

Introduction User Datagram Protocol (UDP) is a connectionless, lightweight transport layer protocol. Unlike TCP, UDP does not guarantee delivery, order, or error checking beyond the basic checksum. However, this simplicity makes it exceptionally fast and efficient for scenarios where speed outweighs reliability, such as real-time video streaming, online gaming, DNS queries, and local network discovery.

procedure TUDPReceiver.OnDataAvailable(const AData: TBytes; AEndpoint: TEndpoint); var Msg: string; begin Msg := TEncoding.UTF8.GetString(AData); // Handle message (use TThread.Queue if updating UI) end; 1. Message Size UDP has a maximum theoretical payload of 65,507 bytes (due to IP and UDP headers). In practice, keep messages under 1,476 bytes to avoid IP fragmentation, which can cause packet loss. 2. Connectionless Nature Do not rely on a “connection” state. Always handle the possibility of no receiver, and implement application-level acknowledgments if needed. 3. Broadcast and Multicast To send a broadcast (all devices on local subnet): delphi udp

type TUDPPacketHeader = packed record SequenceID: UInt32; PacketType: Byte; // 0 = data, 1 = ack, 2 = heartbeat Timestamp: TDateTime; end; Delphi provides robust support for UDP through both the legacy Indy components and the modern System.Net.Socket unit. Indy is ideal for rapid development and VCL applications, while System.Net.Socket offers better cross-platform compatibility and modern async patterns. Choose UDP when speed, simplicity, and broadcast capability are essential, but always implement application-level reliability when data integrity matters.

type TForm1 = class(TForm) IdUDPServer1: TIdUDPServer; procedure IdUDPServer1UDPRead(AThread: TIdUDPListenerThread; const AData: TIdBytes; ABinding: TIdSocketHandle); end; procedure TForm1.IdUDPServer1UDPRead(AThread: TIdUDPListenerThread; const AData: TIdBytes; ABinding: TIdSocketHandle); var ReceivedString: string; RemoteIP: string; RemotePort: Integer; begin ReceivedString := TEncoding.UTF8.GetString(AData); RemoteIP := ABinding.PeerIP; RemotePort := ABinding.PeerPort; // Process the message (e

To send raw bytes:

For production code, consider using a higher-level abstraction or message queue, but for many real-time and discovery scenarios, UDP in Delphi is both efficient and elegant. procedure TUDPReceiver

UDPClient.Host := '255.255.255.255'; // Limited broadcast // Or use the subnet broadcast, e.g., '192.168.1.255' To enable broadcast on the socket:

procedure SendUDPBytes(const AHost: string; APort: Integer; const Bytes: TBytes); var UDPClient: TIdUDPClient; begin UDPClient := TIdUDPClient.Create(nil); try UDPClient.Host := AHost; UDPClient.Port := APort; UDPClient.Send(TIdBytes(Bytes)); finally UDPClient.Free; end; end; The server component operates asynchronously using the OnUDPRead event.