绑定到macOS 10.12上的套接字时的NSPOSIXErrorDomain

时间:2017-12-13 16:05:03

标签: swift macos sockets udp cocoaasyncsocket

我在Swift中使用CocoaAsyncSocket绑定到UDP套接字并通过本地网络接收消息。

我正在初始化一个套接字,并尝试绑定到一个端口但是收到NSPOSIXErrorDomain错误。也许表明某种权限问题?

我的代码:

import Cocoa
import CocoaAsyncSocket

@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate, GCDAsyncUdpSocketDelegate {
    func applicationDidFinishLaunching(_ aNotification: Notification) {
        let socket = GCDAsyncUdpSocket.init(delegate: self, delegateQueue: DispatchQueue.main)
        do {
            try socket.bind(toPort: 53401)
        } catch let msg {
            NSLog("Error....\(msg)")
        }
    }
}

完整错误:

Error Domain=NSPOSIXErrorDomain Code=1 "Operation not permitted" UserInfo={NSLocalizedDescription=Operation not permitted, NSLocalizedFailureReason=Error in bind() function}

2 个答案:

答案 0 :(得分:3)

我相信这是生成的Xcode权利,可以防止绑定。我将这些值更改为false,现在绑定工作

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>com.apple.security.app-sandbox</key>
    <false/>
    <key>com.apple.security.files.user-selected.read-only</key>
    <false/>
</dict>
</plist>

答案 1 :(得分:2)

您应该启用其网络功能

macOS Catalina版本10.15.3 中:

enter image description here