2014-02-24 41 views
1

我正在創建一個使用NSNetService發佈服務器的應用程序,並且我遇到了NSNetServiceListenForConnections選項,可以在OS X 10.9中使用publishWithOptions:方法。這個新選項在Apple提供的「What's New in OS X 10.9」頁面中突出顯示。它指出If you send the NSNetServiceListenForConnections option flag in the options value passed to publishWithOptions:, OS X automatically handles all of the connection management for you,但是,我不明白這是一個新的行爲?我現在只需調用publish方法並等待ServerAcceptCallBack,這是由CFSocketCreate方法設置的。我似乎沒有讓這更容易?NSNetService NSNetServiceListenForConnections使用並檢查可用性?

我正在關注來自CocoaEcho示例的some of Apple's code,該示例獲取一個端口並打開CFSocket。我知道你可以通過0作爲initWithDomain: name: port:方法的port參數,但是會選擇一個「隨機」端口,我猜測這不是一個100%安全的事情。我認爲NSNetServiceListenForConnections可能與此有關,但通過描述,它不會。

所以我實際的問題,畢竟散漫:

什麼是NSNetServiceListenForConnections選項實際上和(爲什麼)我應該使用它?

旁邊的問題:如果我應該使用它,我該如何檢查可用性?我被告知要使用if (&NSNetServiceListenForConnections != NULL),但NSNetServiceListenForConnectionsNSUInteger所以我不能獲得地址(通過&

回答

1

使用此選項,您不必打開或管理的所有插座(即無調用CFSocketCreate)。它爲您創建套接字。雖然我在10.9.2中找到它,但如果您在netService上調用stop,則不會正確關閉套接字,但我仍在調查。 (它似乎在10.9.0和10.9.1中關閉它們)。套接字似乎保持打開,直到你退出應用程序。