Delphi UDP Client

Send and receive UDP datagrams from Delphi/C++Builder. IPv4 and IPv6, an optional DTLS session and a SOCKS5 path, and the transport the whole STUN, TURN, ICE and WebRTC stack is built on.

TsgcUDPClient

High-performance UDP datagram client — the foundation under STUN, TURN, ICE, RTCPeerConnection and any custom UDP protocol you write.

Component class

TsgcUDPClient

Protocol

UDP (RFC 768)

Platforms

Windows, macOS, Linux, iOS, Android

Edition

Core / Standard / Professional / Enterprise

Drop the component, send packets

Set Host / Port, Active := True, then call WriteData with the text or the bytes you want to send. Inbound datagrams arrive on OnUDPRead.

uses
  sgcP2P, sgcUDP_Classes, sgcSocket_Classes;

// TFormUDP holds FClient: TsgcUDPClient and FIncoming: TStringList

procedure TFormUDP.CreateClient;
begin
  FClient := TsgcUDPClient.Create(nil);
  FClient.Host := '127.0.0.1';
  FClient.Port := 4000;

  FClient.OnUDPRead := OnUDPRead;

  FClient.Active := True;
  FClient.WriteData('hello over UDP');
end;

procedure TFormUDP.OnUDPRead(Sender: TObject; Socket: TsgcUDPSocket;
  Bytes: TsgcBytes);
var
  oStream: TStringStream;
begin
  // fires on the read thread, marshal before you touch a control
  oStream := TStringStream.Create('');
  try
    oStream.Write(Bytes[0], Length(Bytes));
    FIncoming.Add(oStream.DataString);
  finally
    oStream.Free;
  end;
end;
// includes: sgcP2P.hpp, sgcUDP_Classes.hpp, sgcSocket_Classes.hpp
TsgcUDPClient *Client = new TsgcUDPClient(this);
Client->Host = "127.0.0.1";
Client->Port = 4000;
Client->OnUDPRead = OnUDPRead;

Client->Active = true;
Client->WriteData("hello over UDP");

void __fastcall TFormUDP::OnUDPRead(TObject *Sender,
  TsgcUDPSocket *Socket, TsgcBytes Bytes)
{
  // fires on the read thread, marshal before you touch a control
}

What's inside

A thin, predictable UDP socket layer used as the transport for STUN / TURN / ICE / WebRTC and any custom datagram protocol.

Unicast send/receive

WriteData(text) and WriteData(bytes) queue a datagram to Host / Port, and the overloads that take an address send one anywhere else. OnUDPRead delivers every inbound packet together with the socket it arrived on.

DTLS

Set DTLS := True and fill DTLSOptions to carry the datagrams inside a DTLS session. OnDTLSVerifyPeer hands you the peer certificate so you decide whether to accept it.

SOCKS5 proxy

Proxy carries the datagrams through a SOCKS5 UDP ASSOCIATE. An HTTP proxy is refused with EsgcUDPProxyNotSupported, because only SOCKS5 defines a UDP path.

The socket you read from

Every OnUDPRead carries a TsgcUDPSocket: IP and Port name the sender, LocalIP and LocalPort name the local base that ICE later publishes as a host candidate.

IPv4 / IPv6

IPVersion is handed straight to the underlying socket, so set it to Id_IPv6 when you need the IPv6 path.

Used under the P2P stack

Every higher P2P component (TsgcSTUNClient, TsgcTURNClient, TsgcICEClient, TsgcRTCPeerConnection) uses this client internally as its UDP transport.

Specifications & references

Authoritative sources for the protocol this component implements.

Documentation & Demos

Deep-link to the component reference, grab the ready-to-run demo project, and download the trial.

Online Help — TsgcUDPClient Full property, method and event reference for this component.
Demo Project — Demos\35.P2P\01.UDP_Server_Client Ready-to-run example project. Ships inside the sgcWebSockets package — download the trial below.
Technical Document (PDF) Features, quick start, code samples for Delphi & C++ Builder and primary-source references — this component only.
User Manual (PDF) Comprehensive manual covering every component in the library.
Best value: All-AccessEvery eSeGeCe product, Premium Support included, from €1,059/year.
See All-Access pricing

Ready to Send UDP from Delphi?

Download the free trial and add UDP datagram support to your Delphi applications.