QT--day4(定时器事件、鼠标事件、键盘事件、绘制事件、实现画板、QT实现TCP服务器)

QT实现tcpf服务器代码:(源文件)

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

   //将服务器设置成监听状态
   if(server->listen(QHostAddress::Any,port))
   {
       QMessageBox::information(this,"","服务器启动成功");
   }
   else
   {
       QMessageBox::information(this,"","服务器启动失败");
   }
   //此时服务器已经进入监听状态,如果有客户端发来连接请求,那么该服务器就会自动发射一个newConnection信号
   //我们可以将该信号连接到自定义的槽函数中处理新连接的套接字
   connect(server,&QTcpServer::newConnection,this,&Widget::newConnection_slot);
}

void Widget::newConnection_slot()
{
    qDebug()<<"有新客户连接";
    //获取最新连接的客户端套接字
    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++)
    {
        if(socketlist.at(i)->state()==0)
        {
            socketlist.removeAt(i);
        }
    }
     //遍历客户端套接字,寻找是哪个客户端有数据待读
    for(int i=0;i<socketlist.count();i++)
    {
        //判断当前套接字是否有数据待读
        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);
            }
        }
    }
}

头文件:

cpp 复制代码
#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>
#include <QTcpServer>
#include <QTcpSocket>
#include <QMessageBox>
#include <QDebug>
#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();
    void newConnection_slot();
    void readyRead_slot();

private:
    Ui::Widget *ui;

    QTcpServer *server;
    QList<QTcpSocket *> socketlist;
};
#endif // WIDGET_H
相关推荐
wc_xue_fei_le几秒前
11.11DNS主从服务器
linux·服务器·前端
是Dream呀13 分钟前
华为CANN 8.0深度评测:挑战CUDA生态的AI计算架构
运维·服务器·cann
white-persist27 分钟前
二进制movl及CTF逆向GDB解析:Python(env)环境下dbg从原理到实战
linux·服务器·开发语言·python·网络安全·信息可视化·系统安全
sxjk198728 分钟前
华为IMS系统主要接口备忘
运维·服务器·前端·核心网
Le_ee29 分钟前
Rocky Linux 8 网络配置
linux·运维·服务器
侯小啾1 小时前
在主机使用命令行扫描网络IP
网络·网络协议·tcp/ip
llc的足迹1 小时前
python构建webRTC服务器,coturn搭建中继服务器
服务器·python·webrtc·turn
2501_915106322 小时前
iOS 抓包全流程指南,HTTPS 抓包、TCP 数据流分析与多工具协同的方法论
android·tcp/ip·ios·小程序·https·uni-app·iphone
艾莉丝努力练剑3 小时前
【Linux基础开发工具 (三)】Vim从入门到精通(下):效率翻倍的编辑技巧与个性化配置攻略
linux·运维·服务器·c++·ubuntu·centos·vim
鹿鸣天涯4 小时前
国产服务器操作系统迁移指南
运维·服务器