gpt4 book ai didi

ios - NWConnection SSDP Discovery 未接收数据

转载 作者:行者123 更新时间:2023-11-28 05:46:09 29 4
gpt4 key购买 nike

我正在尝试进行 SSDP 发现广播,但无法从 NWConnection.receive 获取回复数据。

Network.framework 相对较新,没有太多信息。我在这里缺少什么?

SSDP 发现广播已发送,UPnP 设备已回复。 (下面是 Wireshark 截图) enter image description here

    import Foundation
import Network

let connection = NWConnection(host: "239.255.255.250", port: 1_900, using: .udp)

func sendBroadcast() {
let message = """
M-SEARCH * HTTP/1.1
ST: ssdp:all
HOST: 239.255.255.250:1900
MAN: ssdp:discover
MX: 1
""".data(using: .utf8)

connection.send(content: message, completion: .contentProcessed { error in
if let error = error {
print("Send Error: \(error)")
} else {
print("Broadcast sent")
}
}
)
}

connection.stateUpdateHandler = { newState in
switch newState {
case .setup:
print("Connection: Setup")
case .preparing:
print("Connection: Preparing")
case .waiting:
print("Connection: Waiting")
case .ready:
print("Connection: Ready")
sendBroadcast()
case .failed:
print("Connection: Failed")
case .cancelled:
print("Connection: Cancelled")
}
}

connection.receive(minimumIncompleteLength: 2, maximumLength: 4_096) { data, context, isComplete, error in
/// This is never executed
///
print(data ?? "", context ?? "", isComplete, error ?? "")
}

connection.viabilityUpdateHandler = { update in
print(update)
}

connection.betterPathUpdateHandler = { path in
print(path)
}

connection.start(queue: .main)

RunLoop.main.run()

最佳答案

事实证明 Network.framework 尚不支持 UDP 广播(2019 年 2 月) https://forums.developer.apple.com/message/316357#316357

关于ios - NWConnection SSDP Discovery 未接收数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54701499/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com