iPhone 中的端口转发

发布于 2024-11-30 16:25:00 字数 231 浏览 1 评论 0原文

我必须让我的路由器将从服务器(www.server.com:1112)接收到的数据通过某个端口 xxxx 转发到我的 iPhone 应用程序。我不知道。 我的应用程序侦听该端口,但当服务器将数据发送到端口 xxxx 时,路由器拒绝该操作。

我在路由器中进行了手动配置,效果很好。但我不能期望所有用户都这样做,而且我也不能强迫他们进行此类手动配置。任何人都可以通过提供 iPhone 的示例代码来帮助我吗?

提前致谢。

I have to make my router to forward the data received from server(www.server.com:1112) through some port xxxx to my iPhone application. I don't have any idea.
My application listens the port but while server sends the data to the port xxxx, the router rejects that.

I did manual configuration in my router and it works fine. But I can't expect all of the users to do the same and also I can't force them to do such manual configuration. Can anyone help me please by providing a sample code for iPhone.

Thanx in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

懒的傷心 2024-12-07 16:25:00

正如您所说,您不能指望用户在其防火墙上手动打开端口,但您不能指望他们的防火墙首先会允许这样做。

我建议您最好的选择是使用 uPNP 并让兼容的路由器为您设置端口转发,类似于 BitTorrent。我很幸运地使用了 TCMPortMapper 框架,它在诸如 subethaedit。要无耻地从文档中抄袭,打开端口就像这样简单:

TCMPortMapper *pm = [TCMPortMapper sharedInstance];
[pm addPortMapping:
[TCMPortMapping portMappingWithLocalPort:1337 # local port iPhone listens on 
                    desiredExternalPort:13337 # remote port you want router to listen on
                      transportProtocol:TCMPortMappingTransportProtocolTCP
                               userInfo:nil]];
[pm start];

请记住:

  1. 上面的内容需要连续运行(这会阻塞主线程)以使端口保持打开状态。您需要在后台运行它并注册通知。有关更详细的示例,请阅读文档
  2. uPNP 只是“请求”路由器打开给定端口。它可以拒绝,也可能一开始就不支持。尽管您可以在失败时通知用户,但无法保证它会成功。您的应用程序需要满足这一点。

As you say you can't expect users to manually open ports on their firewall, but you can't expect that their firewalls will permit this in the first place.

I'd suggest your best bet is to use uPNP and have compliant routers set up port forwarding for you, similar to BitTorrent. I've had luck with the TCMPortMapper framework, and it's used in production in apps like subethaedit. To shamelessly rip from the documentation, opening a port is as simple as this:

TCMPortMapper *pm = [TCMPortMapper sharedInstance];
[pm addPortMapping:
[TCMPortMapping portMappingWithLocalPort:1337 # local port iPhone listens on 
                    desiredExternalPort:13337 # remote port you want router to listen on
                      transportProtocol:TCMPortMappingTransportProtocolTCP
                               userInfo:nil]];
[pm start];

Bear in mind:

  1. The above needs to be running continuously (which blocks the main thread) for the port to stay open. You'll want to run it in the background and register for notifications. Read the documentation for more detailed examples.
  2. uPNP just 'requests' that a router open a given port. It can refuse, or it may not support it in the first place. There's no way of guaranteeing it will succeed, although you can notify the user if it doesn't. Your app will need to cater for this.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文