用QT写了一个网络调试助手但是服务器端一直接收不到客户端的信号,有没有人能帮我看一下啊?
这是源代码:
这是server.h头文件
#ifndef SERVER_H
#define SERVER_H
#include <QWidget>
#include <QString>
#include <QUdpSocket>
#include <QTcpServer>
#include <QTcpSocket>
#include <QMessageBox>
#include <QTimer>
#include <QTextCharFormat>
#include <QLineEdit>
#define PORTINIT 8888//默认端口号
namespace Ui {
class Server;
}
class Server : public QWidget
{
Q_OBJECT
QTcpServer server;
QTcpSocket socket;
QString targetAddr; //目标地址
int targetPort;//目标端口
public:
explicit Server(QWidget *parent = nullptr);
~Server();
QLineEdit *QLineEdit;
private slots:
void on_Buttonlink_clicked();
void on_Buttonbreak_clicked();
void on_SendButton_clicked();
void on_clearButton_clicked();
void on_recvEdit_copyAvailable(bool b);
void on_sendEdit_copyAvailable(bool b);
void on_NewConnection();
void on_ServerConnected();
void on_ServerDisconnected();
void on_ServerDataReady();
void on_checkBox_2_clicked();
void on_checkBox_3_clicked();
void on_checkBox_clicked();
private:
Ui::Server *ui;
QString currentIp;//用于保存当前IP
QString currentPort;//用于保存当前端口号
bool connectState;//连接状态
QTcpServer *tcpServerUser;//tcp server模式
QTimer *sendTimer;//定时发送
int sendTimerFlag;//定时发送标志位 1开启 0未开启
void initWindow();//对主窗口进行初始化
void initSysSetting();//设置系统默认初始
void initConnect();//初始化信号和槽的连接
bool startServer();//启动服务器
bool getHostIp(QString &ip);//获取本地IP
void getCurrentIp(QString &ip);//获取输入的IP
void getCurrentPort(QString &port);//获取端口号
void forbidNetworkSet(bool state);//当前网络设置连接/断开
void forbidSendSetting(bool state);//禁用发送区设置
};
#endif // SERVER_H
这是server.cpp
#include "server.h"
#include "ui_server.h"
#include <QMessageBox>
#include <QNetworkInterface>
#include <QApplication>
#include <QHostAddress>
#include<QPushButton>
Server::Server(QWidget *parent) :
QWidget(parent),
ui(new Ui::Server)
{
ui->setupUi(this);
initWindow();
initConnect();
initSysSetting();
on_sendEdit_copyAvailable(true);
}
Server::~Server()
{
QList<QTcpSocket *> m_tcps = server.findChildren<QTcpSocket *>();
foreach (QTcpSocket *tcp, m_tcps)
{
tcp->close();
}
delete ui;
}
void Server::on_Buttonlink_clicked()
{
bool ret;
//用户错误提示初始化
if(ui->IPIn->text().isEmpty()||ui->Port->text().isEmpty())
{
QMessageBox::warning(this,"警告","主机地址或端口号不能为空");
return;
}
//连接
getCurrentIp(currentIp);
getCurrentPort(currentPort);
connectState = tcpServerUser->listen(QHostAddress(currentIp), currentPort.toInt());
if(!connectState){
qDebug() <<"错误:"<< tcpServerUser->errorString();
return;
}
//启动服务端
ret=startServer();
if(ret)
forbidNetworkSet(true);
}
void Server::on_Buttonbreak_clicked()
{
//断开连接
if(sendTimerFlag == 1){
on_SendButton_clicked();
}
tcpServerUser->close();
server.close();
forbidNetworkSet(false);
connectState = false;
on_ServerDisconnected();
}
void Server::initWindow()
{
this->setFixedSize(this->width(),this->height());
this->setWindowTitle("网络调试助手");
}
void Server::initSysSetting()
{
//server端初始化
tcpServerUser = new QTcpServer(this);
//循环发送初始化
QRegExp gapRegx("[1-9][0-9][0-9][0-9][0-9][0-9]");
QValidator *gapValidator = new QRegExpValidator(gapRegx, ui->intervEdit);
ui->intervEdit->setValidator(gapValidator);
ui->intervEdit->setText("1000");
sendTimerFlag = 0;
//输入输出文本编辑框清空
ui->recvEdit->clear();
ui->sendEdit->clear();
//限制ip/port编辑器只能输入指定长度数字
QRegExp ipRegx("[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}");
QRegExp portRegx("[0-9][0-9][0-9][0-9][0-9]");
QValidator *ipValidator=new QRegExpValidator(ipRegx, ui->IPIn);
QValidator *portValidator=new QRegExpValidator(portRegx, ui->Port);
ui->IPIn->setValidator(ipValidator);
ui->Port->setValidator(portValidator);
//获取本地IP地址显示到应用程序上
QString ip;
bool stateGetHostIp;
stateGetHostIp = getHostIp(ip);
//若成功获取ip
if(stateGetHostIp){
//再次判断
if(!ip.isEmpty()){
ui->IPIn->setText(ip);
currentIp = ip;
}
}
else{}
//默认端口号初始化
ui->Port->setText(QString::number(PORTINIT));
currentPort = QString::number(PORTINIT);
}
void Server::initConnect()//初始化信号槽
{
connect(&server, SIGNAL(newConnection()), this, SLOT(on_ServerNewConnection()));
}
bool Server::startServer()
{
if(server.listen(QHostAddress::AnyIPv4,ui->Port->text().toInt())) //只监听IPV4的所有客户端
{
ui->Port->setText(QString("%1").arg(server.serverPort()));
return true;
}
else
return false;
}
bool Server::getHostIp(QString &ip)
{
QString ipAdress;
QList<QHostAddress> ipAdressList = QNetworkInterface::allAddresses();//返回主机上找到的所有IP地址
if(ipAdressList.isEmpty())
return false;
for(int i = 0; i < ipAdressList.size(); ++i){
QHostAddress ipAddr = ipAdressList.at(i);
//筛选ipv4地址
if(ipAddr.protocol() == QAbstractSocket::IPv4Protocol){
ip = ipAddr.toString();
return true;
}
}
return true;
}
void Server::getCurrentIp(QString &ip)
{
QString tempIp = ui->IPIn->text();
ip = tempIp;
qDebug() << "当前网络的IP: " << ip;
}
void Server::getCurrentPort(QString &port)
{
QString tempPort = ui->Port->text();
port = tempPort;
qDebug() << "当前端口号: " << port;
}
void Server::forbidNetworkSet(bool state)//禁用当前网络设置
{
ui->chosecomboBox->setEnabled(state);
ui->IPIn->setEnabled(state);
ui->Port->setEnabled(state);
}
void Server::forbidSendSetting(bool state)//禁用发送区设置
{
ui->intervEdit->setEnabled(state);
ui->sendEdit->setEnabled(state);
}
void Server::on_SendButton_clicked()
{
if(sendTimerFlag == 1){
sendTimer->stop();
forbidNetworkSet(true);
sendTimerFlag = 0;
return ;
}
if(!connectState){
QMessageBox::warning(this,"警告","当前无连接对象");
return;
}
on_recvEdit_copyAvailable(true);
}
void Server::on_clearButton_clicked()
{
ui->recvEdit->clear();
ui->sendEdit->clear();
}
void Server::on_recvEdit_copyAvailable(bool b)
{
QString text=ui->sendEdit->toPlainText();
ui->recvEdit->insertPlainText(text+='\n');
}
void Server::on_sendEdit_copyAvailable(bool b)
{
}
void Server::on_NewConnection()
{
qDebug() << "新连接";
QTcpSocket *tcp=server.nextPendingConnection();//获取新的客户端信息
connect(tcp, SIGNAL(connected()), this, SLOT(on_ServerConnected()));
connect(tcp, SIGNAL(disconnected()), this, SLOT(on_ServerDisconnected()));
connect(tcp, SIGNAL(readyRead()), this, SLOT(on_ServerDataReady()));
}
void Server::on_ServerConnected()
{
}
void Server::on_ServerDisconnected()
{
QTcpSocket *tcp = dynamic_cast<QTcpSocket*>(sender());
if( tcp != NULL ) //从连接对象中移除掉
{
qDebug() << "连接断开";
qDebug() << "IP :"<< tcp->peerAddress();
qDebug() << "Port:" << tcp->peerPort();
}
}
void Server::on_ServerDataReady()
{
QTcpSocket* tcp = dynamic_cast<QTcpSocket*>(sender());
if(tcp->peerAddress().toString()!=targetAddr||tcp->peerPort()!=targetPort)
{
targetAddr = tcp->peerAddress().toString();
targetPort = tcp->peerPort();
ui->recvEdit->insertPlainText("data:""\n");
}
ui->recvEdit->moveCursor(QTextCursor::End);//光标移动到当前数据所在位
if(ui->checkBox_2->isChecked())
{
QByteArray data = tcp->readAll();
for(int i=0;i<data.length();i++)
{
//"%1"表示只接收一组,2为字段宽度,16为16进制编码,不足补1,toupper大写
ui->recvEdit->insertPlainText(QString("%1 ").arg((unsigned char)data[i],2,16,QChar('1')).toUpper());
}
if(ui->checkBox_3->isChecked())
{
ui->recvEdit->insertPlainText("\n");
}
}
else
ui->recvEdit->insertPlainText(QString::fromLocal8Bit(tcp->readAll())+"\n");//返回使用 8 位字符串 str 的第一个大小字符初始化的 QString
if(ui->checkBox->isChecked())
{
}
}
void Server::on_checkBox_2_clicked()
{
}
void Server::on_checkBox_3_clicked()
{
}
void Server::on_checkBox_clicked()
{
}
请问怎么才能让客户端的信息返回到服务器端并且在textedit界面显示出来啊?