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的时候,无论用不用在线程里,都要保证可以在线程中使用。

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

家有一枚袁宝

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值