QT 实现服务器客户端搭建

  1. 服务器头文件
cpp 复制代码
#ifndef SER_H
#define SER_H
 
#include <QWidget>
#include<QTcpServer>    //服务器头文件
#include<QTcpSocket>    //客户端头文件
#include<QMessageBox>   //消息对话框
#include<QList>         //链表头文件
 
QT_BEGIN_NAMESPACE
namespace Ui { class Ser; }
QT_END_NAMESPACE
 
class Ser : public QWidget
{
    Q_OBJECT
 
public:
    Ser(QWidget *parent = nullptr);
    ~Ser();
 
private slots:
    void on_connectbtn_clicked();
 
    void newConnection_slot();  //处理newConnection信号的槽函数
    void readyRead_slot();      //处理readyRead信号的槽函数
 
private:
    Ui::Ser *ui;
 
    QTcpServer *server;             //定义服务器指针
    QList<QTcpSocket *> clientList; //定义客户端容器
};
#endif // SER_H
  1. 服务器源文件
cpp 复制代码
#include "ser.h"
#include "ui_ser.h"
 
Ser::Ser(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Ser)
{
    ui->setupUi(this);
 
    server=new QTcpServer(this);    //实例化一个服务器
 
    connect(server,&QTcpServer::newConnection,this,&Ser::newConnection_slot);
}
 
Ser::~Ser()
{
    delete ui;
}
 
//启动服务器按钮对应的槽函数
void Ser::on_connectbtn_clicked()
{
    quint16 port=ui->portbtn->text().toUInt();
    if(server->listen(QHostAddress::Any,port)){
        QMessageBox::information(this,"成功","服务器启动成功");
    }
    else{
        QMessageBox::information(this,"失败","服务器启动失败");
        return;
    }
}
//处理newConnection信号的槽函数
void Ser::newConnection_slot(){
    QTcpSocket *s=server->nextPendingConnection();  //获取最新连接的客户端套接字
 
    clientList.push_back(s);    //将套接字放入客户端链表中
 
    connect(s,&QTcpSocket::readyRead,this,&Ser::readyRead_slot);
}
//处理readyRead信号的槽函数
void Ser::readyRead_slot(){
    for(int i=0;i<clientList.count();i++){
        if(clientList[i]->state()==0){
            clientList.removeAt(i);
        }
    }
 
    for(int i=0;i<clientList.count();i++){
        if(clientList[i]->bytesAvailable()!=0){
            QByteArray msg=clientList[i]->readAll();
            ui->msgwidget->addItem(QString::fromLocal8Bit(msg));
 
            for(int j=0;j<clientList.count();j++){
                clientList[j]->write(msg);
            }
        }
    }
}
  1. 客户端头文件
cpp 复制代码
#ifndef CLI_H
#define CLI_H
 
#include <QWidget>
#include<QTcpSocket>    //客户端头文件
#include<QMessageBox>   //消息对话框
 
QT_BEGIN_NAMESPACE
namespace Ui { class cli; }
QT_END_NAMESPACE
 
class cli : public QWidget
{
    Q_OBJECT
 
public:
    cli(QWidget *parent = nullptr);
    ~cli();
 
private slots:
    void on_connectbtn_clicked();
    void on_sendbtn_clicked();
    void on_disconnectbtn_clicked();
 
    void connected_slot();  //处理connected信号的槽函数
    void readyRead_slot();  //处理readyRead信号的槽函数
    void disconnected_slot();   //处理disconnected信号的槽函数
 
 
private:
    Ui::cli *ui;
 
    QTcpSocket *socket; //定义客户端指针
 
    QString username;   //用户名
};
#endif // CLI_H

4.客户端源文件

cpp 复制代码
#include "cli.h"
#include "ui_cli.h"
 
cli::cli(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::cli)
{
    ui->setupUi(this);
 
    socket=new QTcpSocket(this);    //实例化一个客户端
 
    ui->disconnectbtn->setEnabled(false);
    ui->sendbtn->setEnabled(false);
    ui->msgedit->setEnabled(false);
 
    connect(socket,&QTcpSocket::connected,this,&cli::connected_slot);
 
    connect(socket,&QTcpSocket::readyRead,this,&cli::readyRead_slot);
 
    connect(socket, &QTcpSocket::disconnected, this, &cli::disconnected_slot);
}
 
cli::~cli()
{
    delete ui;
}
//连接服务器按钮对应的槽函数
void cli::on_connectbtn_clicked()
{
    username=ui->usernameedit->text();
    QString ip=ui->ipedit->text();
    quint16 port=ui->portedit->text().toUInt();
 
    socket->connectToHost(ip,port);
}
//处理connected信号的槽函数
void cli::connected_slot(){
    ui->disconnectbtn->setEnabled(true);
    ui->sendbtn->setEnabled(true);
    ui->msgedit->setEnabled(true);
    ui->usernameedit->setEnabled(false);
    ui->ipedit->setEnabled(false);
    ui->portedit->setEnabled(false);
    ui->connectbtn->setEnabled(false);
    QMessageBox::information(this,"成功","成功进入聊天室");
    QString msg=username+": 进入聊天室";
 
    socket->write(msg.toLocal8Bit());
}
//处理readyRead信号的槽函数
void cli::readyRead_slot(){
    QByteArray msg=socket->readAll();
 
    ui->msgwidget->addItem(QString::fromLocal8Bit(msg));
}
//发送信息按钮对应的槽函数
void cli::on_sendbtn_clicked()
{
    QString msg=username+": "+ui->msgedit->text();
 
    socket->write(msg.toLocal8Bit());
 
    ui->msgedit->clear();
}
//断开服务器按钮对应的槽函数
void cli::on_disconnectbtn_clicked()
{
    QString msg=username+": 离开聊天室";
 
    socket->write(msg.toLocal8Bit());
 
    socket->disconnectFromHost();
}
//处理disconnected信号的槽函数
void cli::disconnected_slot(){
    ui->disconnectbtn->setEnabled(false);
    ui->sendbtn->setEnabled(false);
    ui->msgedit->setEnabled(false);
    ui->usernameedit->setEnabled(true);
    ui->ipedit->setEnabled(true);
    ui->portedit->setEnabled(true);
    ui->connectbtn->setEnabled(true);
    QMessageBox::information(this,"退出","退出成功");
}
相关推荐
德迅--文琪3 分钟前
SCDN是服务器吗?SCDN防御服务器有什么特点?
运维·服务器
重生之我要进大厂10 分钟前
LeetCode 876
java·开发语言·数据结构·算法·leetcode
z2023050815 分钟前
linux 之0号进程、1号进程、2号进程
linux·运维·服务器
Amo Xiang26 分钟前
Python 常用模块(四):shutil模块
开发语言·python
秋已杰爱36 分钟前
HTTP中的Cookie与Session
服务器·网络协议·http
Happy鱿鱼43 分钟前
C语言-数据结构 有向图拓扑排序TopologicalSort(邻接表存储)
c语言·开发语言·数据结构
KBDYD101044 分钟前
C语言--结构体变量和数组的定义、初始化、赋值
c语言·开发语言·数据结构·算法
计算机学姐1 小时前
基于python+django+vue的影视推荐系统
开发语言·vue.js·后端·python·mysql·django·intellij-idea
Crossoads1 小时前
【数据结构】排序算法---桶排序
c语言·开发语言·数据结构·算法·排序算法
扎克begod1 小时前
JAVA并发编程系列(9)CyclicBarrier循环屏障原理分析
java·开发语言·python