问题:
项目里需要个很简单的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的时候,无论用不用在线程里,都要保证可以在线程中使用。