我在特定端口上接收消息时遇到问题。发送正常。
代码看起来像这样:
import Foundation
import CocoaAsyncSocket
class InSocket: NSObject, GCDAsyncUdpSocketDelegate {
//let IP = "192.168.1.196"
let PORT:UInt16 = 14000
var isocket:GCDAsyncUdpSocket!
override init(){
super.init()
setupConnection()
}
func setupConnection(){
isocket = GCDAsyncUdpSocket(delegate: self, delegateQueue: DispatchQueue.main)
do{
try isocket.bind(toPort:PORT)
try isocket.beginReceiving()
} catch {print("ErrorReceive")}
}
func udpSocket(sock: GCDAsyncUdpSocket!, didReceiveData data: NSData, fromAddress address: NSData!, withFilterContext filterContext: AnyObject!) {
let str = NSString(data: data as Data, encoding: String.Encoding.ascii.rawValue)
print(str)
}
}
我在网络统计数据中看到应用程序正在接收数据包:
Packets
但是我在控制台中什么都看不到。
有什么想法吗?
最佳答案
在委托函数之前添加:func udpSocket(....)该属性
@objc(udpSocket:didReceiveData:fromAddress:withFilterContext :)
XCode 8将发出警告,但不注意,UDP数据包将到达。
关于ios - 接收UDP消息,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/39882633/