QT: 完成服务器的实现

1> 思维导图

2> 手动完成服务器的实现,并具体程序要注释清楚

Widget.h

cpp 复制代码
#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>
#include <QTcpServer>   //服务器类
#include <QTcpSocket>   //客户端类
#include <QMessageBox>  //对话框类
#include <QList>  //链表容器
#include <QDebug>

QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACE

class Widget : public QWidget
{
    Q_OBJECT

public:
    Widget(QWidget *parent = nullptr);
    ~Widget();



private slots:
    void on_startBtn_clicked();
    void newConnection_slots();  //自定义处理newConnection信号的槽函数
    void readyRead_slot();    //自定义处理readyRead信号的槽函数

private:
    Ui::Widget *ui;

    //定义服务器指针
    QTcpServer *server;

    //定义客户端容器
    QList<QTcpSocket*> socketList;


};
#endif // WIDGET_H

Widget.cpp

cpp 复制代码
#include "widget.h"
#include "ui_widget.h"

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);

    //给服务器指针实例化空间
    server = new QTcpServer(this);

}

Widget::~Widget()
{
    delete ui;
}

//启动服务器按钮对应的槽函数
void Widget::on_startBtn_clicked()
{
    //获取ui界面上的端口号
    quint16 port = ui->portEdit->text().toUInt();


    //将服务器设置成监听状态
    if(server->listen(QHostAddress::Any,port))
    {
        QMessageBox::information(this,"","服务器启动成功");
    }else
    {
        QMessageBox::information(this,"","服务器启动失败");
    }

    //此时服务器已经进入监听状态,如果有客户端发来连接请求,那么该服务器就会自动发送一个newConnect信号
    //我们可以将该信号连接到自定义的槽函数中处理新连接的套接字
    connect(server,&QTcpServer::newConnection,this,&Widget::newConnection_slots);
}

//处理newConnection信号的槽函数的实现
void Widget::newConnection_slots()
{
    qDebug()<<" 有新用户连接 ";

    //获取最新连接的客户端套接字
    QTcpSocket *s = server->nextPendingConnection();

    //将套接字放入到客户端容器中
    socketList.push_back(s);

    //此时,客户端与服务器已经简历起来连接
    //如果有客户端向服务器发来数据,那么该客户端
    connect(s,&QTcpSocket::readyRead,this,&Widget::readyRead_slot);

}

void Widget::readyRead_slot()
{
    //移除无效客户端
    for(int i=0;i<socketList.count();i++)
    {
        //socketList.at(i)->state();    //任意一个客户端的状态
        if(socketList.at(i)->state()== 0)
        {
            //移除该客户端
            socketList.removeAt(i);   //将下标为i的套接字从链表中移除
        }
    }
    for(int i=0;i<socketList.count();i++)
    {
        if(socketList.at(i)->bytesAvailable()!=0)
        {

            QByteArray msg = socketList.at(i)->readAll();

            ui->msgWidget->addItem(QString::fromLocal8Bit(msg));

            for(int j=0;j<socketList.count();j++)
            {
                socketList.at(j)->write(msg);
            }

        }
    }
}
相关推荐
YuMiao6 小时前
gstatic连接问题导致Google Gemini / Studio页面乱码或图标缺失问题
服务器·网络协议
Sinclair3 天前
简单几步,安卓手机秒变服务器,安装 CMS 程序
android·服务器
Rockbean4 天前
用40行代码搭建自己的无服务器OCR
服务器·python·deepseek
茶杯梦轩4 天前
CompletableFuture 在 项目实战 中 创建异步任务 的核心优势及使用场景
服务器·后端·面试
郑州光合科技余经理5 天前
代码展示:PHP搭建海外版外卖系统源码解析
java·开发语言·前端·后端·系统架构·uni-app·php
海天鹰5 天前
【免费】PHP主机=域名+解析+主机
服务器
feifeigo1235 天前
matlab画图工具
开发语言·matlab
dustcell.5 天前
haproxy七层代理
java·开发语言·前端
norlan_jame5 天前
C-PHY与D-PHY差异
c语言·开发语言
多恩Stone5 天前
【C++入门扫盲1】C++ 与 Python:类型、编译器/解释器与 CPU 的关系
开发语言·c++·人工智能·python·算法·3d·aigc