QTcpSocket 在線程中使用的一個小坑

問題:
項目裏需要個很簡單的tcp通訊,都是發些字符,但遇到個問題是在接受數據後發送反饋時,對方可以接到數據,主動發數據時對方一直接不到數據。
網上看了看,跟線程有關,我也確實是在線程裏發的數據,寫了個小例程測試,果然是這裏的原因。
code:

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include "libtcpsocket.h"
class aa//模擬線程類
{
public:
    void run()
    {
        fun("test");
    }
    void regi(std::function<void(QString)> t)
    {
        fun = t;
    }
    std::function<void(QString)> fun;
};

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

private slots:
    void on_pushButton_clicked();

private:
    Ui::MainWindow *ui;
    LibTcpSocket tcp;
    void threadSend(QString);
    aa m_a;
};

#endif // MAINWINDOW_H
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QtConcurrent>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    s_ConfigNet net;
    net.strServerIP = "127.0.0.1";
    net.iServerPort = 6500;
    net.bAutoAppendEnd=true;
    net.bAutoAppendResp=true;
    net.bAutoAppendStart=true;
    net.strStartCMD="test";
    tcp.Init(net);
    tcp.ConnectToServer();

    m_a.regi(std::bind(&MainWindow::threadSend, this, std::placeholders::_1));
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::on_pushButton_clicked()
{
    QtConcurrent::run(m_a, &aa::run);
}

void MainWindow::threadSend(QString str)
{
    tcp.SendData(str.toUtf8().data());
}
bool LibTcpSocket::SendData(const QString &strData)
{
    if(!m_bInit)
    {
        return false;
    }

    QString toSend;
    if(m_tcpInitParam.bAutoAppendStart)
    {
        toSend.append(m_tcpInitParam.strStartCMD+" ");
    }

    toSend.append(strData);

    qDebug() << toSend;

    if(m_tcpInitParam.bAutoAppendEnd)
    {
        toSend.append("\r\n");
    }    

    qDebug() << toSend;

    int writtenSize = m_pTcpSocket->write(toSend.toUtf8());
    if(writtenSize == -1)
    {
        qDebug() << "an error occurred when writing data";
        return false;
    }
    ///////////////////////////////////////////////
    //flush()和waitForBytesWritten()調用一個即可解決在線程裏
    //發送,server沒接到數據的問題
    m_pTcpSocket->flush();
    bool rec =m_pTcpSocket->waitForBytesWritten();
    /////////////////////////////////////////////
    return true;
}

調用邏輯很簡單,實際的發送過程回調進一個線程執行,但不注意就會忽略線程的事,因爲實際發送的函數在主線程裏,會誤以爲和線程無關。
例程很簡單所以一眼就可以發現,但在複雜的程序裏可能要排查很久才能找到,所以在用qt寫socket的時候,無論用不用在線程裏,都要保證可以在線程中使用。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章