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);
            }

        }
    }
}
相关推荐
「QT(C++)开发工程师」14 小时前
C++ 11 常用for循环
开发语言·c++
我还记得那天14 小时前
0 初识C++
开发语言·c++
FfHUCisI15 小时前
Go 编译过程全景
开发语言·后端·golang
FfHUCisI15 小时前
Golang 语法分析与 AST:Parser 与 go/ast
开发语言·后端·golang
lemon_sjdk16 小时前
ObjectProperty
java·开发语言·算法
大模型码小白16 小时前
Spring AI Tool 实现自然语言操作 MySQL 数据库详解
服务器·开发语言·数据库·人工智能·python·mysql·spring
hh95016 小时前
Agent Plan x DeepSeek Harness:Agent 供应链安全与第三方插件审计
java·开发语言·安全·agent plan·adg成都社区·adg社区
「QT(C++)开发工程师」16 小时前
C++ std::move 详解
开发语言·c++
matlab代码16 小时前
基于matlab的玉米种子计数设计(GUI界面)【源码58期】
开发语言·matlab