TsgcUDPCLientEvents › OnUDPRead

OnUDPRead Event

Fires when an incoming UDP datagram has been received from the peer.

Syntax

property OnUDPRead: TsgcUDPReadEvent;
// TsgcUDPReadEvent = procedure(Sender: TObject; Socket: TsgcUDPSocket; Bytes: TsgcBytes) of object

Default Value

Remarks

Fired once per received datagram, passing the raw bytes through the Bytes parameter and the originating Socket (which exposes PeerIP and PeerPort of the sender). The payload is always delivered as bytes; convert it with TEncoding if the application protocol uses text. The thread that fires this event depends on the NotifyEvents property: neAsynchronous marshals through a background queue, neSynchronous uses Synchronize, and neNoSync fires directly on the reader thread. When DTLS is enabled, Bytes contains the decrypted plaintext.

Example

procedure TForm1.oClientUDPRead(Sender: TObject; Socket: TsgcUDPSocket;
  Bytes: TsgcBytes);
begin
  Memo1.Lines.Add(Format('From %s:%d -> %s',
    [Socket.PeerIP, Socket.PeerPort, TEncoding.UTF8.GetString(Bytes)]));
end;

Back to Events