2016-10-05 166 views
2

我在特定端口上接收消息時遇到問題。發送工作正常。接收UDP消息

的代碼看起來像這樣:

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

但我沒有看到一個控制檯東西。 有些想法?

+0

我遇到問題了,你解決了嗎? –

回答

0

添加委託函數之前:FUNC udpSocket(....)該屬性

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

的XCode 8將發出警告,但不支付注意,UDP數據包將到達。