我在特定端口接收消息时遇到问题。发送工作正常。
代码如下:
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(toPortORT)
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
但我在控制台中看不到任何内容。
一些想法?
Best Answer-推荐答案 strong>
在委托(delegate)函数前添加:func udpSocket(....) 那个属性
@objc(udpSocket:didReceiveData:fromAddress:withFilterContext
XCode 8 会发出警告,但不要注意,UDP 数据包会到达。
关于ios - 接收 UDP 消息,我们在Stack Overflow上找到一个类似的问题:
https://stackoverflow.com/questions/39882633/
|