开发者

port forwarding in iPhone

开发者 https://www.devze.com 2023-03-30 08:08 出处:网络
I have to make my router to forward the data received from server(www.server.com:1112) through some 开发者_C百科port xxxx to my iPhone application. I don\'t have any idea.

I have to make my router to forward the data received from server(www.server.com:1112) through some 开发者_C百科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.


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.
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号