iosswiftgcdasyncsocketgcdasyncudpsocket

Swift example for GCDAsyncSocketDelegate


Refer to this post I have some issues while receiving data from GCDAsyncSocket and can't find a working Swift example.

import UIKit
import CocoaAsyncSocket


class DiscoveryViewControllerTest: UIViewController, GCDAsyncSocketDelegate{
    let host = "192.168.55.1"
    let port:UInt16 = 4000

    let cmdDeviceInformation = "?0600\r";
    let cmdDeviceIStandByeExit = "?060B\r";
    let cmdDeviceIStandByeEnter = "?060A\r";
    var mSocket: GCDAsyncSocket!

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        print("Started wifi scanning!\n")

        mSocket = GCDAsyncSocket(delegate: self, delegateQueue: DispatchQueue.main)
        do {
            try mSocket.connect(toHost: host, onPort: port)
        } catch let error {
            print(error)
        }
        print("Connecting to instrument...!\n")
    }

    public func socket(_ socket: GCDAsyncSocket, didConnectToHost host: String, port p:UInt16){
        print("didConnectToHost!\n");

        let data = cmdDeviceIStandByeEnter.data(using: .utf8)
        print("TX: ", terminator: " ")
        print(data! as NSData)
        mSocket.write(data!, withTimeout:10, tag: 0)

        mSocket.readData(withTimeout: -1, tag: 0) //This line was missing!

    }

    public func socket(_ sock: GCDAsyncSocket, didWriteDataWithTag tag: Int) {
        print("didWriteData");
    }

    public func socket(_ sock: GCDAsyncSocket, didReceive trust: SecTrust, completionHandler: @escaping (Bool) -> Void) {
        print("didReceiveData")

        let rxData:Data = Data()
        mSocket.readData(to: rxData, withTimeout: 5, buffer: nil, bufferOffset: 0, tag: 0)
        print("RX: ", terminator: " ")
        print(rxData as NSData)
    }

    public func socket(_ sock: GCDAsyncSocket, didRead: Data, withTag tag:CLong){
        print("didRead!");
    }

    public func socketDidDisconnect(_ sock: GCDAsyncSocket, withError err: Error?) {
        print("didDisconnect!")
    }
}

The connecting and writing method is running but the "didReceive" Method is never called.

Console output:

Started wifi scanning!

Connecting to instrument...!

didConnectToHost!

TX: <3f303630 410d> didWriteData

EDIT I solved my problem and changed the question code to a ready to use example.


Solution

  • I have found my fault. The clue is to enable reading just behind the mSocket.write() function call in socket didConnectToHost(). The complete function looks like this:

    public func socket(_ socket: GCDAsyncSocket, didConnectToHost host: String, port p:UInt16){
        print("didConnectToHost!\n");
    
        let data = cmdDeviceInformation.data(using: .utf8)
        print("TX: ", terminator: " ")
        print(data! as NSData)
        mSocket.write(data!, withTimeout:10, tag: 0)
    
        mSocket.readData(withTimeout: -1, tag: 0) // Add this line
    }
    

    BTW: I edited my question to create a ready to use example for everyone.