我想使用 C# 进行端口转发

发布于 2024-10-19 15:10:18 字数 482 浏览 6 评论 0原文

我想从路由器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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

jJeQQOZ5 2024-10-26 15:10:18

端口转发是在路由器配置内部处理的。
每个端口转发规则必须手动设置,可以通过路由器的 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.

眼趣 2024-10-26 15:10:18

另外你的代码是错误的。您需要检查 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文