Swift中的GCDAsyncSocket没有执行didReceive方法

时间:2018-07-18 16:06:10

标签: swift gcdasyncsocket

我尝试在整个站点中搜索Swift中GCDAsyncSocket遇到的问题

我在Swift example for GCDAsyncSocketDelegate处发现了这个问题,它已连接到TCPIP服务器。试图复制和修改它,但有两个问题:

  1. didReceive不执行
  2. 我在读取服务器发送的文本数据时遇到问题。

    mSocket.readData(to:Data(),withTimeout:-1,maxLength:768,tag:0)

这是我得到的输出:

Started wifi scanning!

didConnectToHost!

didRead!
<>
didRead!
<>
didRead!
<>

下面是我正在使用的整个代码:

import UIKit
import CocoaAsyncSocket

class ViewController: UIViewController, GCDAsyncSocketDelegate {

var host = "127.0.0.1";
var port = "1080";
let cmdDeviceIStandByeEnter = "?0600\r";


var mSocket: GCDAsyncSocket!
var connected: Bool=false;

@IBOutlet weak var btnConnectDisconnect: UIButton!;
@IBOutlet weak var txtAddress: UITextField!;
@IBOutlet weak var txtPort:UITextField!;
override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.

    btnConnectDisconnect.setTitle("Connect", for: .normal);
    connected = false;
}
override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
    print("Started wifi scanning!\n")

    mSocket = GCDAsyncSocket(delegate: self, delegateQueue: DispatchQueue.main)
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

//TCP
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)
    //while true{
    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)
    mSocket.readData(to: rxData, withTimeout: -1, maxLength: 768, tag: 0)
    print("RX: ", terminator: " ")
    print(rxData as NSData)
}

public func socket(_ sock: GCDAsyncSocket, didRead: Data, withTag tag:CLong){
    print("didRead!");
    //print(Data());
    mSocket.readData(withTimeout: -1, tag: 0);
    mSocket.readData(to: Data(), withTimeout: -1, maxLength: 768, tag: 0)
    //print("RX: ", terminator: " ")
    print(Data() as NSData)
}

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

@IBAction func btnConnectDisconnect_Clicked(sender: AnyObject)
{
    //txtAddress.insertText("Hello");
    host = txtAddress.text!;
    port = txtPort.text!;

    if  connected == false{

        do {
            try
                //mSocket.connect(toHost: host, onPort: UInt16(port)!);
            mSocket.connect(toHost: "192.168.43.192", onPort: 1080);
                btnConnectDisconnect.setTitle("Disconnect", for: .normal);
            connected = true;

        } catch let error {
            print(error)
        }


    } else {
        mSocket.disconnect();
        btnConnectDisconnect.setTitle("Connect", for: .normal);
        connected = false;

    }
}
}

该代码尝试从服务器读取768个字节(固定长度和连续消息)

mSocket.readData(to: Data(), withTimeout: -1, maxLength: 768, tag: 0)

期待您的输入,并在此先感谢。

0 个答案:

没有答案