How to use ngrok tcp tunnels with C# Socket

Viewed 624

I need to connect to the local tcp server with help of ngrok. I've tried to connect like this:

Socket _clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
_clientSocket.Connect(IPAddress.Parse("<int>.tcp.ngrok.io"), 10543); // Port of tcp tunnel

But there is the problem, that it is not connecting me.

1 Answers

Download the ngrok file from the website and start it up with the command:

ngrok tcp (your port here)

when you start it up, after the forwarding you should see the domain. copy it without the tcp:// after that you open cmd and type:

ping (ngrok domain eg. 2.tcp.ngrok.io:69420 becomes 2.tcp.ngrok.io)

for the ping to actually work you need to remove the port after the domain then in the cmd you should see an ip, you should see an ip in the output. Copy this ip into the ip address part of the code where you connect to the server. Select the port as the domain port.

Client.Connect(IPAddress.Parse("ip in the cmd output"), port of the ngrok domain);

I believe this works, not entirely sure though but its worth a try.

Edit: I tried what I listed above and the IP seems to keep changing every once in a while, it still seems to be working every time it changes without me updating the ip inside the code

Related