我想使用 C# 进行端口转发
我想从路由器ip到本地ip的端口转发如何?在 c# 中 我有这个使用本地 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);
}
,其中 br=buffer , textbox2 = local , ip r socket 我尝试在此线程中使用代码,但无法连接它们 C# 原始套接字端口转发
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
端口转发是在路由器配置内部处理的。
每个端口转发规则必须手动设置,可以通过路由器的 Web 界面、telnet/SSH 界面或路由器附带的其他软件来设置。
没有通用的方法可以从 C# 应用程序在路由器上创建端口转发,除非您通过 HTTP/Telnet/SSH 连接到路由器并传递必要的命令来创建规则。我不建议您这样做。
此外,当您更改这些设置时,某些路由器需要重新启动,并且您可能需要等待最多 5 分钟才能完全恢复网络/互联网连接。
无论哪种方式,即使您找到了一种方法,从 C# 应用程序修改路由器配置也可能是一个非常糟糕的主意。
也许您可以澄清为什么要转发端口?也许还有替代解决方案。
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.
另外你的代码是错误的。您需要检查 EOS 的 read() 结果,然后中断,否则将结果传递给 write 调用。您不能假设读取已填满缓冲区。
当然,每次迭代分配一个新的 byte[] 缓冲区也是极其浪费的。
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.