首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >QTcpSocket如何返回QString

QTcpSocket如何返回QString
EN

Stack Overflow用户
提问于 2018-07-12 16:59:31
回答 1查看 282关注 0票数 0

我有一个服务: GWT客户端调用QT QTcpSocket函数,向设备发出请求并获得响应(它不能是唯一的响应。我应该等他们所有的人)。

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

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

我现在只有一个决定:

伪代码:

代码语言:javascript
复制
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吗?

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

代码语言:javascript
复制
// 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();
    }



}   
EN

回答 1

Stack Overflow用户

发布于 2018-07-26 09:03:56

这是我找到的最好的解决方案。它能用,但我不喜欢它

代码语言:javascript
复制
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;
        }

    }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51301353

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档