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

效果图:

相关推荐
趣多多代言人1 小时前
从零开始手写嵌入式实时操作系统
开发语言·arm开发·单片机·嵌入式硬件·面试·职场和发展·嵌入式
牧以南歌〆20 小时前
在Ubuntu主机中修改ARM Linux开发板的根文件系统
linux·arm开发·驱动开发·ubuntu
学不动CV了2 天前
ARM单片机启动流程(二)(详细解析)
c语言·arm开发·stm32·单片机·51单片机
XMAIPC_Robot2 天前
基于ARM+FPGA的光栅尺精密位移加速度测试解决方案
arm开发·人工智能·fpga开发·自动化·边缘计算
学不动CV了2 天前
数据结构---链表结构体、指针深入理解(三)
c语言·arm开发·数据结构·stm32·单片机·链表
szxinmai主板定制专家2 天前
【精密测量】基于ARM+FPGA的多路光栅信号采集方案
服务器·arm开发·人工智能·嵌入式硬件·fpga开发
Paper_Love2 天前
x86-64_windows交叉编译arm_linux程序
arm开发·windows
奇文怪式2 天前
VSCode+arm-none-eabi-gcc交叉编译+CMake构建+OpenOCD(基于Raspberry Pico RP2040)
arm开发·ide·vscode·rp2040
Imagine Miracle2 天前
Ubuntu for ARM 更换为阿里云镜像源
arm开发·ubuntu·阿里云
wwwlyj1233212 天前
arm 精准总线错误与非精准总线错误
arm开发