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.

TsgcTURNClient

TURN client — Allocate, CreatePermission, ChannelBind, Send / Indication / Data, Refresh, all signed with long-term credentials per RFC 8656.

Component class

TsgcTURNClient

Protocol

TURN (RFC 8656)

Platforms

Windows, macOS, Linux, iOS, Android

Edition

Enterprise

Allocate, permission, send

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();

What's inside

A typed RFC 8656 TURN client — the relay layer for WebRTC when STUN-only NAT traversal fails.

Allocate / Refresh

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

CreatePermission(peerIp) registers a peer whose datagrams the relay will forward. Permissions auto-expire after 5 minutes — refresh as needed.

ChannelBind

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.

Long-term credentials

TURNOptions.Authentication.Username and Password populate MESSAGE-INTEGRITY, using the realm and nonce taken from the 401 challenge. Credentials selects short or long term.

TURN over TCP

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.

WebRTC ready

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.

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 — TsgcTURNClient Full property, method and event reference for this component.
Demo Project — Demos\35.P2P\03.TURN 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 Use TURN from Delphi?

Download the free trial and add TURN-based relaying to your Delphi applications.