QTcpSocket如何返回QString

时间:2018-07-12 08:59:31

标签: qt qtcpsocket

我有一项服务:GWT客户端调用QT QTcpSocket函数,该函数向设备发出请求并获取响应(不能只是一个响应。我应该等待所有响应)。

根据QT文档,因为我使用Windows平台,所以不能使用waitForReadyRead()函数。

  

注意:此功能在Windows上可能会随机失败。考虑使用   事件循环和readyRead()信号是否将在您的软件上运行   视窗。 http://doc.qt.io/qt-5/qabstractsocket.html#waitForReadyRead

我现在只有一个决定:

伪代码:

QString MainQTFunc() {

    create new thread;

    while (!thread.isStopped()) {
        sleep(x);
    }

    return QString variable from thread to GWT client;
}



 New Thread {

      run() {
          make a TcpRequest to the device...
      }

    boolean isStopped() {
        if(we got the response!!!) {
            return true;
        }
    }

 }

这是最好的解决方案吗?得到结果后,我不明白如何简单地发送QString变量。强大的QT真的不可能吗?

现在我拥有了(没有任何线程):

// The function should to return QString to the GWT client
QString MainWindow::TcpConnect(QByteArray data) { 

    _pSocket = new QTcpSocket( this ); 
    connect( _pSocket, SIGNAL(readyRead()), SLOT(readTcpData()) );
    connect( _pSocket, SIGNAL(connected()), SLOT(connected()) );
    connect( _pSocket, SIGNAL(disconnected()), SLOT(disconnected()) );
    dataGlobal = data;
    _pSocket->connectToHost("IP", port);

    //waiting here for all responses and sendinig the last response 

    return responseHexGlobal;

}

void MainWindow::connected() {

    qDebug() << "connected. " << QDateTime::currentDateTime();
     _pSocket->write( dataGlobal );

}

void MainWindow::disconnected() {

    qDebug() << "disconnected. " << QDateTime::currentDateTime();

}

void MainWindow::readTcpData()
{
    QByteArray data = _pSocket->readAll();
    QByteArray as_hex_string = data.toHex();

    QString response = QString(as_hex_string);

    if(some condition here...) {
        responseHexGlobal = response;

        _pSocket->disconnectFromHost();
    }



}   

1 个答案:

答案 0 :(得分:0)

这是我找到的最佳解决方案。可以,但是我不喜欢

QString JSPrinter::connectTcp(QByteArray data) {

        QTimer timer;
        timer.setSingleShot(true);
        QEventLoop loop;

        _pSocket = new QTcpSocket( this ); // <-- needs to be a member variable: QTcpSocket * _pSocket;

        connect( _pSocket,  SIGNAL(readyRead()),      SLOT(readTcpData()) );
        connect( _pSocket,  SIGNAL(connected()),      SLOT(connected()) );
        connect( _pSocket,  SIGNAL(disconnected()),   SLOT(disconnected()) );
        connect( &timer,    SIGNAL(timeout()), &loop, SLOT(quit()) );
        loop.connect( this, SIGNAL(exitLoop()),       SLOT(quit()) );

        dataGlobal = data;

        _pSocket->connectToHost(ip_, port_);

        timer.start(75000);
        loop.exec();

        if(timer.isActive())
            logger()->info("ok");
        else
            logger()->info("timeout");

        return responseHexGlobal;

    }



    void JSPrinter::connected() {

        qDebug() << "connected. " << QDateTime::currentDateTime();
        _pSocket->write( dataGlobal );

    }

    void JSPrinter::disconnected() {

        qDebug() << "disconnected. " << QDateTime::currentDateTime();

    }

    void JSPrinter::readTcpData() {

        QByteArray data = _pSocket->readAll();
        QByteArray as_hex_string = data.toHex();
        std::string stdString(as_hex_string.constData(), as_hex_string.length());
        qDebug() << "readTcpData response " << QDateTime::currentDateTime() << QString::fromStdString(stdString);

        // doing something...

        if(condition) {
            responseHexGlobal = received;
            qDebug() << "responseHexGlobal " << responseHexGlobal;
            emit exitLoop();
            _pSocket->disconnectFromHost();

        } else {
            emit exitLoop();
            _pSocket->disconnectFromHost();
            return;
        }

    }