ios – Swift:使用GCDAsyncUdpSocket接收UDP

前端之家收集整理的这篇文章主要介绍了ios – Swift:使用GCDAsyncUdpSocket接收UDP前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
背景:

我想能够在我的iOS应用和服务器之间发送和接收UDP数据包.
服务器回传每个传入的消息给客户端的应用程序.服务器经过测试和确认工作.我有一个StartViewController,它启动两个实现GCDAsyncUdpSocketDelegate的类,一个用于发送,另一个用于接收. “发送套接字”正在工作,服务器收到消息.

问题:

应用程序在发送后不会收到传入的消息.由于didReceiveData从来没有被调用,所以有一些侦听套接字设置可能是错误的.

我这样做完全错了吗?

开始:

class StartViewController: UIViewController {

   var inSocket : InSocket!
   var outSocket : OutSocket!

   override func viewDidLoad() {
       super.viewDidLoad()
       inSocket = InSocket()
       outSocket = OutSocket()
   }

   @IBAction func goButton(sender: UIButton) {
       outSocket.send("This is a message!")
   }
}

接收:

class InSocket: NSObject,GCDAsyncUdpSocketDelegate {

   let IP = "255.255.255.255"
   let PORT:UInt16 = 5556
   var socket:GCDAsyncUdpSocket!

   override init(){
       super.init()
       setupConnection()
   }

   func setupConnection(){
       var error : NSError?
       socket = GCDAsyncUdpSocket(delegate: self,delegateQueue: dispatch_get_main_queue())
       socket.bindToPort(PORT,error: &error)
       socket.enableBroadcast(true,error: &error)
       socket.joinMulticastGroup(IP,error: &error)
       socket.beginReceiving(&error)
   }

   func udpSocket(sock: GCDAsyncUdpSocket!,didReceiveData data: NSData!,fromAddress address: NSData!,withFilterContext filterContext: AnyObject!) {
       println("incoming message: \(data)");
   }
}

发送:

class OutSocket: NSObject,GCDAsyncUdpSocketDelegate {

   let IP = "90.112.76.180"
   let PORT:UInt16 = 5556
   var socket:GCDAsyncUdpSocket!

   override init(){
       super.init()
       setupConnection()
   }

   func setupConnection(){
       var error : NSError?
       socket = GCDAsyncUdpSocket(delegate: self,delegateQueue: dispatch_get_main_queue())
       socket.connectToHost(IP,onPort: PORT,error: &error)
   }

   func send(message:String){
       let data = message.dataUsingEncoding(NSUTF8StringEncoding)
       socket.sendData(data,withTimeout: 2,tag: 0)
   }

   func udpSocket(sock: GCDAsyncUdpSocket!,didConnectToAddress address: NSData!) {
       println("didConnectToAddress");
   }

   func udpSocket(sock: GCDAsyncUdpSocket!,didNotConnect error: NSError!) {
       println("didNotConnect \(error)")
   }

   func udpSocket(sock: GCDAsyncUdpSocket!,didSendDataWithTag tag: Int) {
       println("didSendDataWithTag")
   } 

   func udpSocket(sock: GCDAsyncUdpSocket!,didNotSendDataWithTag tag: Int,dueToError error: NSError!) {
        println("didNotSendDataWithTag")
   }
}

编辑:
添加了忘记的代码行.

解决方法

我终于得到它使用这个套接字设置:
func setupConnection(){
    var error : NSError?
    socket = GCDAsyncUdpSocket(delegate: self,delegateQueue: dispatch_get_main_queue())
    socket.bindToPort(PORT,error: &error)
    socket.connectToHost(SERVER_IP,error: &error)
    socket.beginReceiving(&error)
    send("ping")
}

func send(message:String){
   let data = message.dataUsingEncoding(NSUTF8StringEncoding)
   socket.sendData(data,tag: 0)
}
原文链接:https://www.f2er.com/iOS/329558.html

猜你在找的iOS相关文章