Home Delphi: TCP Hole Punching
Reply: 0

Delphi: TCP Hole Punching

Andrew
1#
Andrew Published in 2017-09-13 17:34:14Z

I'm trying to do TCP Hole Punching in Delphi. I have only one port opened - 10000 (for testing purposes).

Here is a snippet of my code (full source code):

procedure TFormMain.btnEnableRedirectClick(Sender: TObject);
begin
  IdTCPServerRetr.Active:=True;
end;

procedure TFormMain.btnConnectToClientClick(Sender: TObject);
begin
  if IdTCPClientRemote.Port = 0 then
    ShowMessage('Wait for connection to redirect server...')
  else begin
    IdTCPClientRemote.Host:=MyIP;
    IdTCPClientRemote.Connect;
    ShowMessage('S U C C E S S');
  end;
end;

procedure TFormMain.btnConnectClick(Sender: TObject);
begin
  IdTCPClientLocal.Host:=MyIP;
  IdTCPClientLocal.Connect;
  ShowMessage('Connected to redirect server!');
end;

procedure TFormMain.btnListenClick(Sender: TObject);
begin
  IdTCPServerLocal.DefaultPort:=IdTCPClientLocal.Socket.Binding.Port;
  IdTCPServerLocal.Active:=True;
  ShowMessage('Local server started!');
end;

procedure TFormMain.IdTCPServerRetrExecute(AContext: TIdContext);
begin
  IdTCPClientRemote.Port:=AContext.Connection.Socket.Binding.PeerPort;
end;

First, I run the remote server (for redirection) on port 10000.

IdTCPServerRetr.Active:=True;

After that, I connect to the redirect server (port 10000) and create local server, using the same port as at client.

IdTCPClientLocal.Connect; IdTCPServerLocal.DefaultPort:=IdTCPClientLocal.Socket.Binding.Port; IdTCPServerLocal.Active:=True;

(client and server have a reusesocket = rsTrue)

After that, I try to connect to the client, on its internal port, but I get the error "Connection Timeout".

I think the problem is that the remote client can't find the server to connect, since the client has already established a connection with the redirect server.

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.308 second(s) , Gzip On .

© 2016 Powered by cudou.com design MATCHINFO