Delphi TURN Client
Talk to a TURN server from Delphi/C++Builder. Allocate relayed transport addresses, manage permissions and channels, then exchange data with peers behind symmetric NAT — per RFC 8656.
Talk to a TURN server from Delphi/C++Builder. Allocate relayed transport addresses, manage permissions and channels, then exchange data with peers behind symmetric NAT — per RFC 8656.
TURN client — Allocate, CreatePermission, ChannelBind, Send / Indication / Data, Refresh, all signed with long-term credentials per RFC 8656.
TsgcTURNClient
Windows, macOS, Linux, iOS, Android
Enterprise
Set the TURN server, put the credentials on TURNOptions.Authentication, call Allocate, then CreatePermission for each peer and SendIndication to relay data.
uses
sgcP2P, sgcP2P_STUN_Classes, sgcP2P_STUN_Types, sgcP2P_TURN_Classes,
sgcSocket_Classes;
// TFormTURN holds FClient: TsgcTURNClient and FLog: TStringList
procedure TFormTURN.StartAllocation;
begin
FClient := TsgcTURNClient.Create(nil);
FClient.Host := 'turn.example.com';
FClient.Port := 3478;
FClient.TURNOptions.Authentication.Credentials := stauLongTermCredential;
FClient.TURNOptions.Authentication.Username := 'alice';
FClient.TURNOptions.Authentication.Password := 'secret';
FClient.OnTURNAllocate := OnTURNAllocate;
FClient.Allocate;
end;
procedure TFormTURN.OnTURNAllocate(Sender: TObject;
const aSocket: TsgcSocketConnection;
const aMessage: TsgcSTUN_Message;
const aAllocation: TsgcTURN_ResponseAllocation);
begin
FLog.Add('relay: ' + aAllocation.RelayedIP + ':' +
IntToStr(aAllocation.RelayedPort));
// the relay only forwards for a peer you have permissioned
FClient.CreatePermission('192.0.2.1');
FClient.SendIndication('192.0.2.1', 5000, 'hello via TURN');
end;
// includes: sgcP2P.hpp, sgcP2P_STUN_Types.hpp, sgcP2P_TURN_Classes.hpp
TsgcTURNClient *Client = new TsgcTURNClient(this);
Client->Host = "turn.example.com";
Client->Port = 3478;
Client->TURNOptions->Authentication->Credentials = stauLongTermCredential;
Client->TURNOptions->Authentication->Username = "alice";
Client->TURNOptions->Authentication->Password = "secret";
Client->OnTURNAllocate = OnTURNAllocate;
Client->Allocate();
A typed RFC 8656 TURN client — the relay layer for WebRTC when STUN-only NAT traversal fails.
Allocate sends the Allocate request with REQUESTED-TRANSPORT and obtains a relayed transport address, which arrives on OnTURNAllocate as aAllocation.RelayedIP and RelayedPort. Refresh(lifetime) extends it and Refresh(0) deallocates.
CreatePermission(peerIp) registers a peer whose datagrams the relay will forward. Permissions auto-expire after 5 minutes — refresh as needed.
ChannelBind(peerIp, peerPort) bypasses the 36-byte Send/Data overhead. The component picks the channel number, encodes the 4-byte ChannelData header, and reports the binding on OnTURNChannelBind.
TURNOptions.Authentication.Username and Password populate MESSAGE-INTEGRITY, using the realm and nonce taken from the 401 challenge. Credentials selects short or long term.
Transport := stunTCP moves the control channel to TCP for networks that block UDP, which is the RFC 6062 path. TURNOptions.AutoRefresh then keeps the allocation, the permissions and the channels alive on its own.
TsgcICEClient drives one through its TURNClient property, and TsgcRTCPeerConnection allocates from the turn: entries of RTCOptions.ICEServers. The Allocate, CreatePermission and ChannelBind sequence happens for you.
Deep-link to the component reference, grab the ready-to-run demo project, and download the trial.
| Online Help — TsgcTURNClient Full property, method and event reference for this component. | Open | |
| Demo Project — Demos\35.P2P\03.TURN 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 |