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

        }
    }
}
相关推荐
lsx202406几秒前
SVN 检出操作详解
开发语言
一晌小贪欢5 分钟前
Python 对象的“Excel 之旅”:使用 openpyxl 高效读写与封装实战
开发语言·python·excel·表格·openpyxl·python办公·读取表格
赵八斤7 分钟前
java 项目中配置多个数据源
java·开发语言·数据库
ArrebolJiuZhou13 分钟前
03 rtp,rtcp,sdp的包结构
linux·运维·服务器·网络·arm开发
txinyu的博客15 分钟前
解析muduo源码之 StringPiece.h
开发语言·网络·c++
浅念-16 分钟前
C语言——单链表
c语言·开发语言·数据结构·经验分享·笔记·算法·leetcode
2501_9445264218 分钟前
Flutter for OpenHarmony 万能游戏库App实战 - 关于页面实现
android·java·开发语言·javascript·python·flutter·游戏
Dem119 分钟前
怎么安装jdk
java·开发语言
输出输入20 分钟前
那鸿蒙应用的后端服务器用什么语言编写
服务器·华为
wazmlp00188736922 分钟前
python第一次作业
开发语言·python·算法