Qt tcp 多线程下QTCPsocket不能发送数据~
工程文件MainWindow.pro文件~
在实现QTCPsocket的基本功能(连接服务器,(不循环)发送数据)~
但是接受不到数据(有readyread()信号)
添加qt类 mythread类;并且在mainwindow头文件中包含此类的头文件
(类中包含QTCPsocket文件和QTCPsocket *p_socket指针,和要发生的数据char sendbuff[])
在mainwindow类中 实现线程循环时,实例一个mythread的对象,并对其赋值;
mythread.senbuf = mainwindow的数组
mythread.p_sendbuff = mainwindow的QTCPsocket 指针
我企图用上面的代码来传递指针和要发送的数组;
但是代码怎么也发送不过去~~(mythread数组的值已经传递过来,但是指针的值我不敢确定)
还有就是在mainwindow类中的QTCPsocket 指针读不出来数据(有readyread()信号);
到了现在我实在是无招了~~
所以上来问问,请大仙们给点指导和意见~
还请大仙不灵赐教 (附代码~)
[code=C/C++]
//MAINWINDOW 头文件
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QSettings>
#include <QMainWindow>
#include <QAction>
#include <QtNetwork/QTcpSocket>
#include <configdialog.h>
//#include <QByteArray>
#include <mythread.h>
#include <defaultdialog.h>
struct message_stream{
unsigned char protocol_version; //1BYTE
QByteArray message_type; //2BYTE
QByteArray message_length; //2BYTE
QByteArray parameter_type; //2BYTE
QByteArray parameter_length; //2BYTE
QByteArray parameter_Value; //不定长
};
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
QSettings *p_longfile; //
QTcpSocket *p_socketet;
QTcpSocket *P_thr_socket;
QByteArray qba_message; //recv到的数据
MyThread mthread; //
message_stream mag_stra; //
char m_buffer[8];
int blockSize; //发送/接收文件的大小
bool autosendstat;
bool GenerateCW( char *buffer); //生成控制字
void SocketTCPstart();
void SockSendData(char *buffer);
void analyzingbuff(QByteArray qb_recvarrat);
private slots:
void SockRecvData(); //接收数据
void socketerror(); //socker错误
void connectError(); //连接服务器错误
void on_pushButton_send_clicked();
void on_action_socket_close_triggered();
void on_action_socket_open_triggered();
void on_action_exit_triggered();
void on_action_serverconfig_triggered();
void on_action_default_triggered();
void on_action_rand_triggered();
void on_action_define_triggered();
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
//MAINWINDOW cpp文件
/////////////////////////mainwindow.cpp///////////////
#include "mainwindow.h "
#include "ui_mainwindow.h "
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui-> setupUi(this);
p_socketet = new QTcpSocket(this);
autosendstat = false;
memset(m_buffer,0,sizeof(m_buffer));
//connect(,SIGNAL(),this,SLOT());
connect(p_socketet,SIGNAL(readyRead()),this,SLOT(SockRecvData())); //有数据来
connect(p_socketet,SIGNAL(disconnect()),this,SLOT(connectError()));
connect(p_socketet,SIGNAL(error(QAbstractSocket::SocketError)),this,SLOT(socketerror()));
ui-> pushButton_send-> setEnabled(false);
SocketTCPstart();
}
MainWindow::~MainWindow()
{
delete ui;
delete p_socketet;
}
void MainWindow::SocketTCPstart()
{
p_longfile = new QSettings( "ClientConfig.ini ",QSettings::IniFormat,0);
QString temp_STR = p_longfile-> value( "CLIENT_CONFIG/SERVERIP ").toString();
int temp_I = p_longfile-> value( "CLIENT_CONFIG/SERVERPORT ").toUInt();
if(temp_STR.isEmpty()||(temp_I == 0))
{
ui-> label_dissata-> setText( "错误:client启动失败!可能是配置文件出错! ");
return;
}
else
ui-> pushButton_send-> setEnabled(true);
p_socketet-> connectToHost(temp_STR,temp_I,QIODevice::WriteOnly);
delete p_longfile;
}
bool MainWindow::GenerateCW(char *buffer)
{
......
}
void MainWindow::SockSendData(char *buffer)
{
// GenerateCW(buffer);
p_socketet-> write(buffer,8);
}
void MainWindow::on_pushButton_send_clicked()
{
if( GenerateCW(m_buffer))
{
if(mthread.b_threadsata == true)
{
ui-> pushButton_send-> setText( "发送 ");
mthread.b_threadsata = false;
mthread.exit();
ui-> lineEdit-> clear();
}
if(ui-> lineEdit-> text().isEmpty()==false)
{
mthread.b_threadsata = false;
mthread.p_sock = p_socketet;
mthread.sendnumber =ui-> lineEdit-> text().toUInt();
mthread.p_senddata =m_buffer;
mthread.start();
// mthread.wait();
// ui-> pushButton_send-> setText( "停止 ");
}
else if(ui-> lineEdit-> text().isEmpty())
{
SockSendData(m_buffer);
}
}
}
void MainWindow::on_action_socket_close_triggered()
{
.....
}
void MainWindow::on_action_socket_open_triggered()
{
......
}
void MainWindow::on_action_exit_triggered()
{
exit(0);
}
void MainWindow::on_action_serverconfig_triggered()
{
.....
}
void MainWindow::on_action_default_triggered()
{
....
}
void MainWindow::on_action_rand_triggered()
{
.....
}
void MainWindow::on_action_define_triggered()
{
.....
}
void MainWindow::socketerror()
{
.......
}
void MainWindow::SockRecvData()
{
QString qs_str;
QByteArray qb_bay;
char buf[64] = {0};
qb_bay = p_socketet-> readAll(); //虽然有信号但是读不到数据
//qba_message.clear();
}
void MainWindow::connectError()
{
p_socketet-> abort();
ui-> label_dissata-> setText( "错误:连接服务器失败! ");
ui-> pushButton_send-> setEnabled(false);
return;
}
void MainWindow::analyzingbuff(QByteArray qb_recvarrat)
{
mag_stra.protocol_version = qb_recvarrat[0];
}
//MYTHREAD头文件
#ifndef MYTHREAD_H
#define MYTHREAD_H
#include <QTcpSocket>
#include <QThread>
class MyThread : public QThread
{
Q_OBJECT
public:
explicit MyThread(QObject *parent = 0);
void run();
void qrandcw(char *p_buff);
QTcpSocket * p_sock;
QString qrandcd_str;
int sendnumber;
char *p_senddata;
bool b_threadsata;
char buffer[8];
signals:
public slots:
};
#endif // MYTHREAD_H
//mythread cpp文件
#include "mythread.h "
#include <QSettings>
MyThread::MyThread(QObject *parent) :
QThread(parent)
{
b_threadsata = false;
memset(buffer,0,sizeof(buffer));
QSettings p_file;
qrandcd_str =p_file.value( "CW_DEFAULT/DEFAULT ").toString();
}
void MyThread::run()
{
if (sendnumber == 0)
return;
for(int i = 0;i <8 ; i ++)
{
buffer[i]=p_senddata[i];
}
for(int i = 0;i <3 ; i ++)
{
if(qrandcd_str.isEmpty())
{
memset(buffer,0,sizeof(buffer));
qrandcw(buffer);
}
long int I =p_sock-> write(buffer,sizeof(buffer));
//p_sock-> atEnd();
// msleep(1000/1);
if(I ==-1)
return;
}
}
void MyThread::qrandcw(char *p_buff)
{
QString s_temp;
bool ok=false;
p_buff[0] = s_temp.setNum(qrand()%16).toAscii().toUInt(&ok,16);
p_buff[1] = s_temp.setNum(qrand()%16).toAscii().toUInt(&ok,16);
p_buff[2] = s_temp.setNum(qrand()%16).toAscii().toUInt(&ok,16);
p_buff[4] = s_temp.setNum(qrand()%16).toAscii().toUInt(&ok,16);
p_buff[5] = s_temp.setNum(qrand()%16).toAscii().toUInt(&ok,16);
p_buff[6] = s_temp.setNum(qrand()%16).toAscii().toUInt(&ok,16);
}
//还有其他几个实现对话框的文件(无关紧要)
[/code]
[解决办法]
调试啊,
尽量缩小问题的范围,看问题到底在哪里
确定哪里的代码错了问题。
一般可以用qDebug()来输出相关信息。
也可以用QMessageBox();
[解决办法]
没有格式看得我头疼 你方便发我么 刚做完tcp udp socket