2023/10/4 -- ARM

今日任务:QT实现TCP服务器客户端搭建的代码,现象

ser:

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);
    connect(server,&QTcpServer::newConnection,this,&Widget::newConnectionSlot);

}

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


void Widget::on_startButton_clicked()
{
    quint16 port = ui->portEdit->text().toUInt();
    //监听客户端的连接请求
    if(!server->listen(QHostAddress::Any,port)){
        QMessageBox::information(this,"失败","监听失败");
        return;
    }else{
        QMessageBox::information(this,"成功","服务器启动成功");
    }
}

void Widget::newConnectionSlot()
{
    //qDebug()<<"您有新的客户端发来请求";

    QTcpSocket *s = server->nextPendingConnection();
    clientList.push_back(s);
    connect(s,&QTcpSocket::readyRead,this,&Widget::readyReadSlot);
}

void Widget::readyReadSlot()
{
    for(int i = 0;i < clientList.size();i++){
        //判断当前套接字是否有效连接
        if(!clientList[i]->state()){
            //将该套接字移除客户端容易
            clientList.removeAt(i);
        }
    }
    for(int i = 0;i < clientList.size();i++){
        if(clientList[i]->bytesAvailable()){
            //将该套接字的数据读取出来
            QByteArray msg = clientList[i]->readAll();
            ui->msgWidget->addItem(QString::fromLocal8Bit(msg));

            for(int j = 0;j < clientList.size();j++){
                clientList[j]->write(msg);
            }
        }
    }
}

cli:

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);
    connect(server,&QTcpServer::newConnection,this,&Widget::newConnectionSlot);

}

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


void Widget::on_startButton_clicked()
{
    quint16 port = ui->portEdit->text().toUInt();
    //监听客户端的连接请求
    if(!server->listen(QHostAddress::Any,port)){
        QMessageBox::information(this,"失败","监听失败");
        return;
    }else{
        QMessageBox::information(this,"成功","服务器启动成功");
    }
}

void Widget::newConnectionSlot()
{
    //qDebug()<<"您有新的客户端发来请求";

    QTcpSocket *s = server->nextPendingConnection();
    clientList.push_back(s);
    connect(s,&QTcpSocket::readyRead,this,&Widget::readyReadSlot);
}

void Widget::readyReadSlot()
{
    for(int i = 0;i < clientList.size();i++){
        //判断当前套接字是否有效连接
        if(!clientList[i]->state()){
            //将该套接字移除客户端容易
            clientList.removeAt(i);
        }
    }
    for(int i = 0;i < clientList.size();i++){
        if(clientList[i]->bytesAvailable()){
            //将该套接字的数据读取出来
            QByteArray msg = clientList[i]->readAll();
            ui->msgWidget->addItem(QString::fromLocal8Bit(msg));

            for(int j = 0;j < clientList.size();j++){
                clientList[j]->write(msg);
            }
        }
    }
}

效果图:

相关推荐
AndyHeee2 天前
【SVC、PendSV(系统异常) 与 外设 IRQ 、NVIC笔记】
arm开发
暮云星影2 天前
瑞芯微rk3588利用Rockchip NPU运行大语言模型(LLM)
arm开发·人工智能·语言模型·自然语言处理
techdashen2 天前
绕过系统 ICMP:用 rawsock、Npcap 和 WMI 找到默认网卡
开发语言·arm开发·rust
振南的单片机世界2 天前
ARM中断比51快在哪?硬件压栈+NVIC集中管理
arm开发·stm32·单片机·嵌入式硬件
墨绿色的摆渡人2 天前
论文笔记(一百三十七)Learning Dual-Arm Push and Grasp Synergy in Dense Clutter
arm开发·论文阅读
暮云星影3 天前
全志linux开发屏幕适配(一)屏幕参数设置说明
linux·arm开发
m0_547486663 天前
《ARM Cortex-M4嵌入式应用技术——基于STM32F407、STM32CubeMX与Proteus》全套PPT课件
arm开发·stm32·proteus
Lanceli_van3 天前
SQLite 3.45.2(sqlite-autoconf-3450200)ARM 交叉编译完整步骤
arm开发·sqlite
暮云星影3 天前
全志linux开发屏幕适配(二)`HDMI`驱动适配说明
linux·arm开发·驱动开发
暮云星影3 天前
瑞芯微rk3566开发FIT Secure Boot
linux·arm开发·驱动开发·安全