接收UDP消息

时间:2016-10-05 19:56:55

标签: ios swift udp swift3 gcdasyncudpsocket

我在特定端口上接收消息时遇到问题。发送工作正常。

代码如下:

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

但我在控制台中看不到任何东西。 一些想法?

1 个答案:

答案 0 :(得分:0)

在委托函数之前添加:func udpSocket(....)该属性

@objc(udpSocket:didReceiveData:FROMADDRESS:withFilterContext:)

XCode 8会发出警告,但不注意,UDP数据包会到达。