6.19作业

网络聊天室服务器实现

cpp 复制代码
//头文件

#ifndef WIDGET_H
#define WIDGET_H

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

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

public slots:
    void newConnection_slot();
    void readyRead_slot();

private:
    Ui::Widget *ui;
    //实例化一个服务器指针
    QTcpServer *server;
    //定义一个存放客户端套接字的容器
    QList<QTcpSocket *> socketList;
};
#endif // WIDGET_H



//源文件

#include "widget.h"
#include "ui_widget.h"

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
    , server(new QTcpServer(this))//给服务器指针实例化空间
{
    ui->setupUi(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,"","启动服务器失败!");
        return;
    }
    //此时服务器已设置好监听,如果有客户端发来连接请求,那么服务器端就会自动发射一个newConnection()信号
    //将该信号连接到自定义的槽函数中,处理逻辑代码
    connect(server,&QTcpServer::newConnection,this,&Widget::newConnection_slot);
}

//newConnection_slot信号对应的槽函数实现
void Widget::newConnection_slot()
{
    //使用nextpaddingConnection获取最新连接客户端的套接字
    //函数原型:virtual QTcpSocket *nextPendingConnection();
   QTcpSocket *s = server->nextPendingConnection();

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

   //程序运行至此说明服务器和客户端已经建立了连接
   //如果有客户端向服务器发来数据,则客户端就会自动发射一个readyRead信号,
   //就可以将该信号连接到自定义的槽函数中,读取数据
   connect(s,&QTcpSocket::readyRead,this,&Widget::readyRead_slot);
}

void Widget::readyRead_slot()
{
    //遍历客户端容器,移除无效客户端
    for(int i=0; i<socketList.count(); i++)//count为容器的元素个数
    {
        //state函数判断连接状态
        //函数原型:SocketState state() const;
        //函数返回值,枚举值为0的表示未连接
        if(socketList.at(i)->state() == 0)
        {
            //删除该元素
            socketList.removeAt(i);
        }
    }
    //遍历客户端容器寻找哪个客户端有数据待读
    for(int i=0; i<socketList.count(); i++)
    {
        //函数功能:数据的字节
        //函数原型:qint64 bytesAvailable() const override;
        if(socketList.at(i)->bytesAvailable()!=0)
        {
            QByteArray msg = socketList.at(i)->readAll();
            //将读取到的数据放入ui界面上
            ui->listWidget->addItem(QString::fromLocal8Bit(msg));
            //将数据广播给所有客户端
            for(int j=0; j<socketList.count(); j++)
            {
                socketList.at(j)->write(msg);
            }
        }
    }
}
相关推荐
Gold Steps.1 分钟前
真实企业级K8S故障案例:ETCD集群断电恢复与数据保障实践
服务器·k8s·高可用·故障恢复
阿巴~阿巴~8 分钟前
蓝桥杯 C/C++ 组历届真题合集速刷(一)
c语言·c++·算法·蓝桥杯
s_little_monster32 分钟前
【Linux】线程控制函数
linux·运维·服务器·经验分享·笔记·学习·学习方法
czhc11400756632 小时前
网络3 子网掩码 划分ip地址
服务器·网络·tcp/ip
不爱学英文的码字机器2 小时前
[操作系统] 进程间通信:system V共享内存
linux·服务器·ubuntu
秋名RG2 小时前
计算机网络起源
服务器·网络·计算机网络
努力的搬砖人.2 小时前
nacos配置达梦数据库驱动源代码步骤
java·服务器·数据库·经验分享·后端
努力学习的小廉2 小时前
【智能指针】—— 我与C++的不解之缘(三十三)
开发语言·c++
baobao17676408302 小时前
C++单例模式
javascript·c++·单例模式
shenxiaolong_code2 小时前
编译器bug ?
c++·bug·meta programming·compiler bug