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.
Listen for UDP datagrams on a port and dispatch each one to your application. The same engine that powers TsgcSTUNServer and TsgcTURNServer.
High-performance UDP listener — bind to a port, receive datagrams from any peer, reply to specific endpoints, all through one bound socket.
TsgcUDPServer
Windows, macOS, Linux, iOS, Android
Professional / Enterprise
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));
}
A UDP listener that hands every datagram to your handler with the TsgcUDPSocket it arrived on, which is also the reply path.
One bound socket handles datagrams from any number of remote endpoints. Each OnUDPRead fires with a TsgcUDPSocket whose IP and Port identify the sender.
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 restarts the listener when it stops answering, and OnBeforeWatchDog gives you the chance to veto the restart. OnStartup and OnShutdown bracket every bind.
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.
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.
Set DTLS := True and fill DTLSOptions to run the datagrams inside a DTLS session, with OnDTLSVerifyPeer deciding which peer certificates you accept.
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. | Open | |
| Demo Project — Demos\35.P2P\01.UDP_Server_Client Ready-to-run example project. Ships inside the sgcWebSockets package — download the trial below. | Open | |
| Technical Document (PDF) Features, quick start, code samples for Delphi & C++ Builder and primary-source references — this component only. | Open | |
| User Manual (PDF) Comprehensive manual covering every component in the library. | Open |