Swift: Receive UDP with GCDAsyncUdpSocket

|▌冷眼眸甩不掉的悲伤 提交于 2019-11-27 13:00:02

问题


BACKGROUND:

I want to be able to send and receive UDP packets between my iOS app and a server. The server echoes back every incoming message to the client the app. The server is tested and confirmed working. I have a StartViewController which starting up two classes that implements GCDAsyncUdpSocketDelegate, one for sending and one for receiving. The "sending socket" is working, the server receives the messages.

PROBLEM:

The app never get the incoming message back after it been sent. Something with the listening socket setup is probably wrong since didReceiveData never get called.

Have I done this completely wrong?

Start:

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!")
   }
}

Receive:

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)");
   }
}

Send:

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")
   }
}

Edit: Added forgotten code line.


回答1:


I finally got it to work with this socket setup:

func setupConnection(){
    var error : NSError?
    socket = GCDAsyncUdpSocket(delegate: self, delegateQueue: dispatch_get_main_queue())
    socket.bindToPort(PORT, error: &error)
    socket.connectToHost(SERVER_IP, onPort: PORT, error: &error)
    socket.beginReceiving(&error)
    send("ping")
}

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



回答2:


Apple Swift version 4.2.1 Well Tested UDP Example:-

STEP 1 :- pod 'CocoaAsyncSocket'

STEP 2 :- import CocoaAsyncSocket in your UIViewController.

STEP 3 :- UIViewController

        import UIKit
import CocoaAsyncSocket

class ViewController: UIViewController {
    @IBOutlet weak var btnOnOff: LightButton!
    @IBOutlet weak var lblStatus: UILabel!
    var inSocket : InSocket!
    var outSocket : OutSocket!
    override func viewDidLoad() {
        super.viewDidLoad()
        lblStatus.isHidden = true
        inSocket = InSocket()
        outSocket = OutSocket()
        outSocket.setupConnection {
            self.lblStatus.isHidden = false
        }
    }
    @IBAction func btnLight(_ sender: Any) {
        let signal:Signal = Signal()
        self.outSocket.send(signal: signal)
    }
}

STEP 4 :- Reciving Socket

       //Reciving End...
class InSocket: NSObject, GCDAsyncUdpSocketDelegate {
   //let IP = "10.123.45.2"
    let IP = "127.0.0.1"
    let PORT:UInt16 = 5001
    var socket:GCDAsyncUdpSocket!
    override init(){
        super.init()
        setupConnection()
    }
    func setupConnection(){
        socket = GCDAsyncUdpSocket(delegate: self, delegateQueue:DispatchQueue.main)
        do { try socket.bind(toPort: PORT)} catch { print("")}
        do { try socket.enableBroadcast(true)} catch { print("not able to brad cast")}
        do { try socket.joinMulticastGroup(IP)} catch { print("joinMulticastGroup not procceed")}
        do { try socket.beginReceiving()} catch { print("beginReceiving not procceed")}
    }
    //MARK:-GCDAsyncUdpSocketDelegate
    func udpSocket(_ sock: GCDAsyncUdpSocket, didReceive data: Data, fromAddress address: Data, withFilterContext filterContext: Any?) {
          print("incoming message: \(data)");
          let signal:Signal = Signal.unarchive(d: data)
          print("signal information : \n first \(signal.firstSignal) , second \(signal.secondSignal) \n third \(signal.thirdSignal) , fourth \(signal.fourthSignal)")

    }
    func udpSocket(_ sock: GCDAsyncUdpSocket, didNotConnect error: Error?) {
    }

    func udpSocketDidClose(_ sock: GCDAsyncUdpSocket, withError error: Error?) {
    }
}

STEP 5 :- Sending Socket..

//Sending End...
class OutSocket: NSObject, GCDAsyncUdpSocketDelegate {
    // let IP = "10.123.45.1"
    let IP = "127.0.0.1"
    let PORT:UInt16 = 5001
    var socket:GCDAsyncUdpSocket!
    override init(){
        super.init()

    }
    func setupConnection(success:(()->())){
        socket = GCDAsyncUdpSocket(delegate: self, delegateQueue:DispatchQueue.main)
          do { try socket.bind(toPort: PORT)} catch { print("")}
          do { try socket.connect(toHost:IP, onPort: PORT)} catch { print("joinMulticastGroup not procceed")}
          do { try socket.beginReceiving()} catch { print("beginReceiving not procceed")}
        success()
    }
    func send(signal:Signal){
        let signalData = Signal.archive(w: signal)
        socket.send(signalData, withTimeout: 2, tag: 0)
    }
    //MARK:- GCDAsyncUdpSocketDelegate
    func udpSocket(_ sock: GCDAsyncUdpSocket, didConnectToAddress address: Data) {
        print("didConnectToAddress");
    }
    func udpSocket(_ sock: GCDAsyncUdpSocket, didNotConnect error: Error?) {
        if let _error = error {
            print("didNotConnect \(_error )")
        }
    }
    func udpSocket(_ sock: GCDAsyncUdpSocket, didNotSendDataWithTag tag: Int, dueToError error: Error?) {
          print("didNotSendDataWithTag")
    }
    func udpSocket(_ sock: GCDAsyncUdpSocket, didSendDataWithTag tag: Int) {
        print("didSendDataWithTag")
    }
}

STEP 6 :- Your Signal Data which you will Send/Recieve

import Foundation
struct Signal {
    var firstSignal:UInt16 = 20
    var secondSignal:UInt16 = 30
    var thirdSignal: UInt16  = 40
    var fourthSignal: UInt16 = 50
    static func archive(w:Signal) -> Data {
        var fw = w
        return Data(bytes: &fw, count: MemoryLayout<Signal>.stride)
    }
    static func unarchive(d:Data) -> Signal {
        guard d.count == MemoryLayout<Signal>.stride else {
            fatalError("BOOM!")
        }
        var s:Signal?
        d.withUnsafeBytes({(bytes: UnsafePointer<Signal>)->Void in
            s = UnsafePointer<Signal>(bytes).pointee
        })
        return s!
    }
}


来源:https://stackoverflow.com/questions/26790129/swift-receive-udp-with-gcdasyncudpsocket

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!