【发布时间】:2019-07-05 13:31:27
【问题描述】:
我正在尝试连接到本地 esp8266 UDP 服务器。 SwiftSocket 没有文档。 CocoaAsyncSocket 不起作用。 如何连接并发送数据到 udp 服务器?我该怎么办?
我编写了示例 UDP python 服务器并尝试通过 SwiftSocket 和 CocoaAsyncSocket 连接到它们。我没有从应用程序中获得反馈。服务器不接收连接。 例如 - 最多的尝试之一:
var connection = NWConnection(host: "255.255.255.255", port: 9093, using: .udp)
connection.stateUpdateHandler = { (newState) in
switch (newState) {
case .ready:
print("ready")
case .setup:
print("setup")
case .cancelled:
print("cancelled")
case .preparing:
print("Preparing")
default:
print("waiting or failed")
break
}
}
connection.start(queue: .global())
connection.send(content: "Xyu".data(using: String.Encoding.utf8), completion: NWConnection.SendCompletion.contentProcessed(({ (NWError) in
print(NWError)
})))
connection.receiveMessage { (data, context, isComplete, error) in
print("Got it")
print(data)
}
无法连接到服务器
【问题讨论】: