Delphi UDP Server

Listen for UDP datagrams on a port and dispatch each one to your application. The same engine that powers TsgcSTUNServer and TsgcTURNServer.

TsgcUDPServer

High-performance UDP listener — bind to a port, receive datagrams from any peer, reply to specific endpoints, all through one bound socket.

Component class

TsgcUDPServer

Protocol

UDP (RFC 768)

Platforms

Windows, macOS, Linux, iOS, Android

Edition

Professional / Enterprise

Drop, listen, reply

Set Port, Active := True, then handle inbound datagrams in OnUDPRead and reply through Socket.WriteData — one socket, any number of remote peers.

uses
  sgcP2P, sgcUDP_Classes, sgcSocket_Classes;

// TFormUDPServer holds FServer: TsgcUDPServer and the handler

procedure TFormUDPServer.StartServer;
begin
  FServer := TsgcUDPServer.Create(nil);
  FServer.Port := 4000;

  FServer.OnUDPRead := OnUDPRead;

  FServer.Active := True;
end;

procedure TFormUDPServer.OnUDPRead(Sender: TObject;
  Socket: TsgcUDPSocket; Bytes: TsgcBytes);
begin
  // echo back to the peer the datagram came from
  Socket.WriteData(TBytes(Bytes));
end;
// includes: sgcP2P.hpp, sgcUDP_Classes.hpp, sgcSocket_Classes.hpp
TsgcUDPServer *Server = new TsgcUDPServer(this);
Server->Port = 4000;
Server->OnUDPRead = OnUDPRead;
Server->Active = true;

void __fastcall TFormUDPServer::OnUDPRead(TObject *Sender,
  TsgcUDPSocket *Socket, TsgcBytes Bytes)
{
  // echo back to the peer the datagram came from
  Socket->WriteData(TBytes(Bytes));
}

What's inside

A UDP listener that hands every datagram to your handler with the TsgcUDPSocket it arrived on, which is also the reply path.

Single-port many-peers

One bound socket handles datagrams from any number of remote endpoints. Each OnUDPRead fires with a TsgcUDPSocket whose IP and Port identify the sender.

Reply path

Socket.WriteData sends a datagram back to the originating peer over the same socket, and the server also carries WriteData(ip, port, ...) for any other endpoint.

WatchDog

WatchDog restarts the listener when it stops answering, and OnBeforeWatchDog gives you the chance to veto the restart. OnStartup and OnShutdown bracket every bind.

IPv4 / IPv6 and several bindings

Set IPVersion for the family, or add one entry per address to Bindings with AddBinding, RemoveBinding and GetBinding when the server has to listen on several.

Used under TsgcSTUNServer / TsgcTURNServer

The STUN and TURN servers (full RFC 8489 / 8656 implementations) build on top of this component — you can do the same for any custom UDP protocol.

DTLS

Set DTLS := True and fill DTLSOptions to run the datagrams inside a DTLS session, with OnDTLSVerifyPeer deciding which peer certificates you accept.

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 — TsgcUDPServer 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 Run a UDP Server?

Download the free trial and listen for UDP datagrams from Delphi.