TsgcUDPServerMethods › WriteData

WriteData Method

Sends a single UDP datagram from the server to a specific peer address and port, optionally from a chosen local binding.

Overloads

Overload 1

Syntax

procedure WriteData(const aIP: string; aPort: Word; const aValue: string);

Parameters

NameTypeDescription
aIPconst stringDestination IPv4/IPv6 address of the peer (typically the PeerIP reported by OnUDPRead).
aPortWordDestination UDP port of the peer in the range 1 to 65535.
aValueconst stringText payload. Encoded as UTF-8 bytes before being transmitted.

Remarks

Replies to (or proactively sends to) a UDP peer using the first available local binding. UDP is connectionless, so the server does not keep a persistent connection per peer: the destination address must be supplied on every call. The typical pattern is to read aIP/aPort from the Socket parameter of OnUDPRead and echo a response back. When DTLS is enabled the datagram is encrypted using the cached DTLS session for that peer.

Example

procedure TForm1.oServerUDPRead(Sender: TObject; Socket: TsgcUDPSocket;
  Bytes: TsgcBytes);
begin
  oServer.WriteData(Socket.PeerIP, Socket.PeerPort, 'ack');
end;

Overload 2

Syntax

procedure WriteData(const aIP: string; aPort: Word; const aBytes: TBytes);

Parameters

NameTypeDescription
aIPconst stringDestination IPv4/IPv6 address of the peer.
aPortWordDestination UDP port of the peer in the range 1 to 65535.
aBytesconst TBytesRaw binary payload sent verbatim inside the UDP datagram.

Remarks

Binary counterpart of the string overload: no character-set conversion is applied, which is convenient for serialized protocol frames, audio samples or other non-text data. Datagram size should stay below the path MTU (typically 1472 bytes on IPv4 Ethernet) to avoid IP fragmentation and lost datagrams.

Example

var vBytes: TBytes;
begin
  vBytes := TEncoding.UTF8.GetBytes('pong');
  oServer.WriteData('192.168.1.10', 5000, vBytes);
end;

Overload 3

Syntax

procedure WriteData(const aSourceIP: string; const aSourcePort: Word; const aIP: string; aPort: Word; const aValue: string);

Parameters

NameTypeDescription
aSourceIPconst stringLocal IP of the binding that should send the datagram. Must match an IP from Bindings.
aSourcePortconst WordLocal UDP port of the binding that should send the datagram.
aIPconst stringDestination IPv4/IPv6 address of the peer.
aPortWordDestination UDP port of the peer in the range 1 to 65535.
aValueconst stringText payload. Encoded as UTF-8 bytes before being transmitted.

Remarks

Explicit-source variant used when the server listens on multiple bindings and the reply must go out through a specific interface (for example, to keep the conversation on the same IP that received the datagram). The server looks up the TIdSocketHandle whose IP and port match aSourceIP and aSourcePort and sends the datagram from that socket; if no match is found the first available binding is used as a fallback.

Example

procedure TForm1.oServerUDPRead(Sender: TObject; Socket: TsgcUDPSocket;
  Bytes: TsgcBytes);
begin
  oServer.WriteData(Socket.LocalIP, Socket.LocalPort,
    Socket.PeerIP, Socket.PeerPort, 'ack');
end;

Overload 4

Syntax

procedure WriteData(const aSourceIP: string; const aSourcePort: Word; const aIP: string; aPort: Word; const aBytes: TBytes);

Parameters

NameTypeDescription
aSourceIPconst stringLocal IP of the binding that should send the datagram.
aSourcePortconst WordLocal UDP port of the binding that should send the datagram.
aIPconst stringDestination IPv4/IPv6 address of the peer.
aPortWordDestination UDP port of the peer in the range 1 to 65535.
aBytesconst TBytesRaw binary payload sent verbatim inside the UDP datagram.

Remarks

Explicit-source, binary-payload variant. Combines the behaviour of overloads 2 and 3: use it when the server has several bindings and the reply is a binary blob that must be sent verbatim from a specific local endpoint.

Example

var vBytes: TBytes;
begin
  vBytes := TEncoding.UTF8.GetBytes('pong');
  oServer.WriteData('192.168.1.10', 5000, '192.168.1.20', 6000, vBytes);
end;

Back to Methods