QTcpSocket如何返回QString

Posted

技术标签:

【中文标题】QTcpSocket如何返回QString【英文标题】:QTcpSocket how to return QString 【发布时间】:2018-07-12 08:59:31 【问题描述】:

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

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

注意:此功能在 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】:

这是我找到的最佳解决方案。它有效,但我不喜欢它

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;
        

    

【讨论】:

以上是关于QTcpSocket如何返回QString的主要内容,如果未能解决你的问题,请参考以下文章

QTcpSocket->bytesAvailable 总是返回 0

C++ Qt - QTcpSocket - 找不到文件

QT 使用 QTcpSocket来检测 ip 设备的网络状态

Qt - 当一个类中有多个 QTcpSocket 时,我如何知道哪个 QTcpSocket 发出了 readyRead 信号?

QTcpSocket - 如何发送两个数字

如何安全地删除 QT::QTcpSocket?