一、运行结果
二、程序代码
1、服务器端
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QTcpServer>
#include <QTcpSocket>
#include <QLabel>
QT_BEGIN_NAMESPACE
namespace Ui {
class MainWindow;
}
QT_END_NAMESPACE
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
private slots:
void on_setListen_clicked();
void on_sendMsg_clicked();
private:
Ui::MainWindow *ui;
QTcpServer* m_s;
QTcpSocket* m_tcp;
QLabel* m_status;
};
#endif // MAINWINDOW_H
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
ui->port->setText("8899");
setWindowTitle("服务器");
//创建监听的服务器对象
m_s=new QTcpServer(this);
//m_s 这个 QTcpServer 对象接收到一个新的连接请求时,会发出 newConnection 信号
connect(m_s,&QTcpServer::newConnection,this,[=](){
//新的连接请求到达时,m_s->nextPendingConnection() 会返回一个QTcpSocket对象
m_tcp=m_s->nextPendingConnection();
m_status->setPixmap(QPixmap(":/connect.png").scaled(20,20));
//QTcpSocket 对象接收到可读数据时,会发出readyRead信号
connect(m_tcp,&QTcpSocket::readyRead,this,[=](){
QByteArray data = m_tcp->readAll();
ui->record->append("客户端说:"+data);
});
//QTcpSocket 对象断开连接时,会发出 disconnected 信号
connect(m_tcp,&QTcpSocket::disconnected,this,[=](){
m_tcp->close();
m_tcp->deleteLater();
m_status->setPixmap(QPixmap(":/disconnect.png").scaled(20,20));
});
});
//状态栏
m_status =new QLabel;
m_status->setPixmap(QPixmap(":/disconnect.png").scaled(20,20));
ui->statusbar->addWidget(new QLabel("连接状态:"));
ui->statusbar->addWidget(m_status);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_setListen_clicked()
{
unsigned short port=ui->port->text().toUShort();
m_s->listen(QHostAddress::Any,port);//QTcpServer对象 m_s会开始监听指定的IP地址和端口号,等待客户端的连接请求
ui->setListen->setDisabled(true);
}
void MainWindow::on_sendMsg_clicked()
{
QString msg=ui->msg->toPlainText();
m_tcp->write(msg.toUtf8());
ui->record->append("服务器say:"+msg);
}
2、客户端
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QTcpSocket>
#include <QLabel>
QT_BEGIN_NAMESPACE
namespace Ui {
class MainWindow;
}
QT_END_NAMESPACE
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
private slots:
void on_sendMsg_clicked();
void on_connect_clicked();
void on_disconnect_clicked();
private:
Ui::MainWindow *ui;
QTcpSocket* m_tcp;
QLabel* m_status;
};
#endif // MAINWINDOW_H
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
ui->port->setText("8899");
ui->ip->setText("127.0.0.1");
setWindowTitle("客户端");
ui->disconnect->setDisabled(true);
//创建监听的服务器对象
m_tcp=new QTcpSocket;
connect(m_tcp,&QTcpSocket::readyRead,this,[=](){
QByteArray data=m_tcp->readAll();
ui->record->append("服务器说:"+data);
});
connect(m_tcp,&QTcpSocket::disconnected,this,[=](){
m_tcp->close();
m_tcp->deleteLater();
m_status->setPixmap(QPixmap(":/disconnect.png").scaled(20,20));
ui->record->append("服务器已经和服务器断开了连接.......");
ui->connect->setDisabled(false);
ui->disconnect->setEnabled(false);
});
connect(m_tcp,&QTcpSocket::connected,this,[=](){
m_status->setPixmap(QPixmap(":/connect.png").scaled(20,20));
ui->record->append("已经成功连接到了服务器.......");
ui->connect->setDisabled(true);
ui->disconnect->setEnabled(true);
});
//状态栏
m_status =new QLabel;
m_status->setPixmap(QPixmap(":/disconnect.png").scaled(20,20));
ui->statusbar->addWidget(new QLabel("连接状态:"));
ui->statusbar->addWidget(m_status);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_sendMsg_clicked()
{
QString msg=ui->msg->toPlainText();
m_tcp->write(msg.toUtf8());
ui->record->append("客户端say:"+msg);
}
void MainWindow::on_connect_clicked()
{
QString ip=ui->ip->text();
unsigned short port=ui->port->text().toUShort();
m_tcp->connectToHost(QHostAddress(ip),port);
}
void MainWindow::on_disconnect_clicked()
{
// m_tcp->close();
m_tcp->disconnectFromHost();
m_tcp->abort(); // 强制断开连接
// m_tcp->setSocketState(QAbstractSocket::UnconnectedState);
// m_tcp->deleteLater();
// m_tcp = nullptr;
ui->connect->setDisabled(false);
ui->disconnect->setEnabled(false);
}