TsgcUDPServer › Methods › WriteData
Sends a single UDP datagram from the server to a specific peer address and port, optionally from a chosen local binding.
procedure WriteData(const aIP: string; aPort: Word; const aValue: string);
| Name | Type | Description |
|---|---|---|
aIP | const string | Destination IPv4/IPv6 address of the peer (typically the PeerIP reported by OnUDPRead). |
aPort | Word | Destination UDP port of the peer in the range 1 to 65535. |
aValue | const string | Text payload. Encoded as UTF-8 bytes before being transmitted. |
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.
procedure TForm1.oServerUDPRead(Sender: TObject; Socket: TsgcUDPSocket;
Bytes: TsgcBytes);
begin
oServer.WriteData(Socket.PeerIP, Socket.PeerPort, 'ack');
end;
procedure WriteData(const aIP: string; aPort: Word; const aBytes: TBytes);
| Name | Type | Description |
|---|---|---|
aIP | const string | Destination IPv4/IPv6 address of the peer. |
aPort | Word | Destination UDP port of the peer in the range 1 to 65535. |
aBytes | const TBytes | Raw binary payload sent verbatim inside the UDP datagram. |
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.
var vBytes: TBytes;
begin
vBytes := TEncoding.UTF8.GetBytes('pong');
oServer.WriteData('192.168.1.10', 5000, vBytes);
end;
procedure WriteData(const aSourceIP: string; const aSourcePort: Word; const aIP: string; aPort: Word; const aValue: string);
| Name | Type | Description |
|---|---|---|
aSourceIP | const string | Local IP of the binding that should send the datagram. Must match an IP from Bindings. |
aSourcePort | const Word | Local UDP port of the binding that should send the datagram. |
aIP | const string | Destination IPv4/IPv6 address of the peer. |
aPort | Word | Destination UDP port of the peer in the range 1 to 65535. |
aValue | const string | Text payload. Encoded as UTF-8 bytes before being transmitted. |
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.
procedure TForm1.oServerUDPRead(Sender: TObject; Socket: TsgcUDPSocket;
Bytes: TsgcBytes);
begin
oServer.WriteData(Socket.LocalIP, Socket.LocalPort,
Socket.PeerIP, Socket.PeerPort, 'ack');
end;
procedure WriteData(const aSourceIP: string; const aSourcePort: Word; const aIP: string; aPort: Word; const aBytes: TBytes);
| Name | Type | Description |
|---|---|---|
aSourceIP | const string | Local IP of the binding that should send the datagram. |
aSourcePort | const Word | Local UDP port of the binding that should send the datagram. |
aIP | const string | Destination IPv4/IPv6 address of the peer. |
aPort | Word | Destination UDP port of the peer in the range 1 to 65535. |
aBytes | const TBytes | Raw binary payload sent verbatim inside the UDP datagram. |
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.
var vBytes: TBytes;
begin
vBytes := TEncoding.UTF8.GetBytes('pong');
oServer.WriteData('192.168.1.10', 5000, '192.168.1.20', 6000, vBytes);
end;