i want to make port forwarding using c#

1.7k Views Asked by At

i want to make port forwarding from router ip to local ip how ? in c# i have this code for receiving using local ip

r.Bind(new IPEndPoint(IPAddress.Any, int.Parse(this.textBox2.Text)));
while (true)
            {
                br = new byte[16384];
                r.Receive(br);
                m_Fifo.Write(br, 0, br.Length);
            }

where br=buffer , textbox2 = local , ip r socket I tried using code in this thread but I failed to connect them C# Raw Sockets Port Forwarding

2

There are 2 best solutions below

0
On BEST ANSWER

Port forwarding is handled inside your router configuration. Each port forwarding rule must be set up manually, either through the router's web interface, telnet/SSH interface, or some other software which comes with the router.

There is no general way to create a port forward on your router from a C# application, unless you connect to it through HTTP/Telnet/SSH and pass the necessary commands to create the rule. I do not recommend you do this.

Also, some routers need to be rebooted when you change these settings and you may need to wait up to 5 minutes before your network/Internet connection is fully restored.

Either way, even if you figure out a way to do so, it is probably a very bad idea to tinker with your router configuration from your C# application.

Maybe you can clarify why you want to forward a port? Perhaps there is an alternative solution.

0
On

Also your code is wrong. You need to check the result of the read() for EOS, and break, or else pass the result to the write call. You can't assume the read filled the buffer.

Also of course allocating a new byte[] buffer per iteration is extremely wasteful.