Delphi STUN Client
Discover your reflexive endpoint behind NAT with a typed STUN (RFC 8489) client. Used as the bootstrap step for ICE candidate gathering and WebRTC.
Discover your reflexive endpoint behind NAT with a typed STUN (RFC 8489) client. Used as the bootstrap step for ICE candidate gathering and WebRTC.
STUN client — Binding requests, XOR-MAPPED-ADDRESS extraction, MESSAGE-INTEGRITY / FINGERPRINT support, retransmission timer per RFC 8489.
TsgcSTUNClient
Windows, macOS, Linux, iOS, Android
Standard / Professional / Enterprise
Set the STUN server host and port, call SendRequest, then read aBinding.RemoteIP and aBinding.RemotePort in OnSTUNResponseSuccess.
uses
sgcP2P, sgcP2P_STUN_Classes, sgcSocket_Classes;
// TFormSTUN holds FClient: TsgcSTUNClient and FLog: TStringList
procedure TFormSTUN.Discover;
begin
FClient := TsgcSTUNClient.Create(nil);
FClient.Host := 'stun.l.google.com';
FClient.Port := 19302;
FClient.OnSTUNResponseSuccess := OnSTUNResponseSuccess;
FClient.SendRequest;
end;
procedure TFormSTUN.OnSTUNResponseSuccess(Sender: TObject;
const aSocket: TsgcSocketConnection;
const aMessage: TsgcSTUN_Message;
const aBinding: TsgcSTUN_ResponseBinding);
begin
// RemoteIP and RemotePort carry the XOR-MAPPED-ADDRESS the server saw
FLog.Add('public: ' + aBinding.RemoteIP + ':' +
IntToStr(aBinding.RemotePort));
end;
// includes: sgcP2P.hpp, sgcP2P_STUN_Classes.hpp
TsgcSTUNClient *Client = new TsgcSTUNClient(this);
Client->Host = "stun.l.google.com";
Client->Port = 19302;
Client->OnSTUNResponseSuccess = OnSTUNResponseSuccess;
Client->SendRequest();
A typed RFC 8489 STUN client with the message-integrity and fingerprint extensions used by ICE.
SendRequest sends the STUN 0x0001 Binding message to Host / Port, and SendBindingRequest(host, port) queries another server through the same socket. The reply arrives on OnSTUNResponseSuccess with XOR-MAPPED-ADDRESS already parsed.
Set STUNOptions.Authentication.Credentials to stauShortTermCredential or stauLongTermCredential, fill Username and Password, and the component computes MESSAGE-INTEGRITY. The realm and nonce come from the 401 challenge.
STUN over UDP needs application-layer retransmission. RetransmissionOptions.RTO and MaxRetries drive the RFC 8489 §6.2.1 back-off, and Enabled turns it off when you drive the timing yourself.
XOR-MAPPED-ADDRESS comes back in either family. The component decodes both and hands you the text form in aBinding.RemoteIP, with the family in aBinding.IPVersion.
Drive multiple TsgcSTUNClient instances against several STUN servers to gather server-reflexive ICE candidates — one per local interface.
Uses TsgcUDPClient internally, and Transport := stunTCP switches to the TCP path. GetLocalSocketBinding returns the transport address of that socket, which is the RFC 8445 base every candidate gathered through it shares.
Deep-link to the component reference, grab the ready-to-run demo project, and download the trial.
| Online Help — TsgcSTUNClient Full property, method and event reference for this component. | Open | |
| Demo Project — Demos\35.P2P\02.STUN 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 |