qt编译一个服务端程序,编译通过,从编译器run能正常启动,
服务端监听开始后,当客户端接入,输出窗口提示QObject::connect: Cannot connect (null)::readyRead() to TCPServer::recvData();发送消息会奔溃
我第一次运行这两个程序能够运行,消息互通,没有任何问题。几次后一发消息就提示奔溃。
后来我又把服务端跟客户端两个exe单独放到空文件夹里,用windeployqt.exe打包库文件那些,运行,开始几次也能正常通信,后来又开始奔溃。
输出窗口提示无法连接了。下面源码。
#ifndef TCPSERVER_H
#define TCPSERVER_H
#include <QWidget>
#include <QTcpServer>
#include <QTcpSocket>
namespace Ui {
class TCPServer;
}
class TCPServer : public QWidget
{
Q_OBJECT
public:
explicit TCPServer(QWidget *parent = 0);
~TCPServer();
private:
Ui::TCPServer *ui;
QTcpServer *server;
QTcpSocket *socket;
public slots:
void startServer();
void sendMsg();
void connetserver();
void recvData();
};
#endif // TCPSERVER_H
------------------------.CPP-----------------------------
#include "tcpserver.h"
#include "ui_tcpserver.h"
const quint16 port=6666;
TCPServer::TCPServer(QWidget *parent) :
QWidget(parent),
ui(new Ui::TCPServer)
{
ui->setupUi(this);
server=new QTcpServer(this);
connect(ui->pushButton,SIGNAL(clicked()),this,SLOT(startServer()));
connect(ui->pushButton_2,SIGNAL(clicked()),this,SLOT(sendMsg()));
}
TCPServer::~TCPServer()
{
delete ui;
}
void TCPServer::startServer(){
server->listen(QHostAddress::Any,port);
connect(server,SIGNAL(newConnection()),this,SLOT(connetserver()));
}
void TCPServer::connetserver(){
//获取客户端
socket=server->nextPendingConnection();
connect(socket,SIGNAL(readyRead()),this,SLOT(recvData()));
}
void TCPServer::recvData(){
QByteArray array=socket->readAll();
if(!array.isEmpty()){
ui->textEdit->append(QString::fromLocal8Bit(array));
}
}
void TCPServer::sendMsg(){
QString str=ui->textEdit_2->toPlainText();
socket->write(str.toLocal8Bit());
socket->flush();
}