TsgcSTUNClient component.
TsgcSTUNClient is the client that implements the STUN protocol and allows you to send binding requests to STUN servers.
The component allows you to use UDP and TCP as transport. When using UDP as transport, it implements a Retransmission mechanism to re-send requests if the response has not arrived after a short time.
Usually STUN servers run on UDP port 3478 and don't require authentication, so in order to send a STUN request binding, fill the server properties to allow the client to know where to connect and Handle the events where the component will receive the response from server.
Configure the server
Call the method SendRequest, to send a request binding to STUN server.
Handle the events
If the server returns a successful response, the event OnSTUNResponseSuccess will be called and you can access the binding information by reading the aBinding object.
oSTUN := TsgcSTUNClient.Create(nil);
oSTUN.Host := 'stun.sgcwebsockets.com';
oSTUN.Port := 3478;
oSTUN.SendRequest;
procedure OnSTUNResponseSuccess(Sender: TObject; const aSocket: TsgcSocketConnection;
const aMessage: TsgcSTUN_Message; const aBinding: TsgcSTUN_ResponseBinding);
begin
DoLog('Remote IP: ' + aBinding.RemoteIP + '. Remote Port: ' + IntToStr(aBinding.RemotePort));
end;
procedure OnSTUNResponseError(Sender: TObject; const aSocket: TsgcSocketConnection;
const aMessage: TsgcSTUN_Message; const aError: TsgcSTUN_ResponseError);
begin
DoLog('Error: ' + IntToStr(aError.Code) + ' ' + aError.Reason);
end;