使うもの:
調べてみたら「TCMPortMapper」というフレームワークがあるそうです。UPnPかNAT-PMPに対応しているルータなら、とても簡単です。サンプルとしてGUIでポートを開くツール「PortMap」も上記からダウンロードできます。
コード:
アプリの起動時に
TCMPortMapper *pm = [TCMPortMapper sharedInstance]; [pm addPortMapping: [TCMPortMapping portMappingWithLocalPort:1234 desiredExternalPort:1234 transportProtocol:TCMPortMappingTransportProtocolTCP userInfo:nil]]; [pm start];
でポートを開き、アプリ終了時に NSApplicationのapplicationWillTerminate: で
[[TCMPortMapper sharedInstance] stopBlocking];
と後片付けをすればOK。
インターフェースを切り替えたりしてネットワーク接続が変更されると自動的に再設定してくれたり、1234番ポートが衝突していると1235を使ったり、といったことも勝手にやってくれます。
簡単、便利。