QT upd测试

QT upd测试

本次测试将服务器和客户端写在了一个工程下,代码如下
widget.h

#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>
#include<QUdpSocket>
#include<QTimer>

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

    void dataReceived();

    void on_pushButton_2_clicked();
    void timeoutslot();

private:
    Ui::Widget *ui;

public:
    int m_iport;
    bool m_bisstarted;
    QUdpSocket *udpSocket_server;
     QUdpSocket *udpSocket_client;
    QTimer *timer;
};
#endif // WIDGET_H

widget.cpp

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

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
   
    ui->setupUi(this);
    m_iport = 5555;
    udpSocket_server = new QUdpSocket(this);
    udpSocket_client = new QUdpSocket(this);
    udpSocket_client->bind(m_iport);
    timer = new QTimer(this);
    connect(timer , SIGNAL(timeout()) , this , SLOT(timeoutslot()));
    connect(udpSocket_client , SIGNAL(readyRead()) , this , SLOT(dataReceived()));
    this->setWindowTitle("updtest");

}

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

void Widget::on_pushButton_clicked()
{
   
    timer->start(500);
}

void Widget::timeoutslot()
{
   
    QString msg = ui->lineEdit_send->text();
    if(msg.size()==0)
    {
   
        return;
    }
    //转成utf8 避免中文乱码
    udpSocket_server->writeDatagram(msg.toUtf8 ().data(), msg.toUtf8().size() , QHostAddress::Broadcast , m_iport);

}


void Widget::on_pushButton_2_clicked()
{
   
    close();
}


void Widget::dataReceived()
{
   
    while(udpSocket_client->hasPendingDatagrams())
    {
   
        QByteArray datagram;
        datagram.resize(udpSocket_client->pendingDatagramSize());
        udpSocket_client->readDatagram(datagram.data() , datagram.size());
        QString msg = datagram.data();
        ui->textEdit->insertPlainText(msg);

    }
}


运行结果如下:
在这里插入图片描述

相关推荐

  1. 软件测试——单元测试

    2024-01-23 07:00:05       57 阅读
  2. 软件测试——集成测试

    2024-01-23 07:00:05       60 阅读
  3. 测试:接口参数测试

    2024-01-23 07:00:05       62 阅读
  4. 接口测试:项目测试

    2024-01-23 07:00:05       48 阅读
  5. 测试】1.认识测试

    2024-01-23 07:00:05       110 阅读
  6. 单元测试、集成测试、系统测试区别

    2024-01-23 07:00:05       39 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-01-23 07:00:05       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-01-23 07:00:05       106 阅读
  3. 在Django里面运行非项目文件

    2024-01-23 07:00:05       87 阅读
  4. Python语言-面向对象

    2024-01-23 07:00:05       96 阅读

热门阅读

  1. 数据结构-学习笔记

    2024-01-23 07:00:05       49 阅读
  2. 如何发布自己的npm包

    2024-01-23 07:00:05       46 阅读
  3. 自然语言处理的发展

    2024-01-23 07:00:05       56 阅读
  4. pytorch模型转caffe模型

    2024-01-23 07:00:05       53 阅读
  5. NPM: 如何修改npm源?

    2024-01-23 07:00:05       49 阅读
  6. bfs广度优先搜索

    2024-01-23 07:00:05       47 阅读
  7. 【Spring Boot 3】【Redis】集成Redisson

    2024-01-23 07:00:05       54 阅读
  8. Unity3D 协程的优缺点详解

    2024-01-23 07:00:05       55 阅读
  9. 网络工程师:新兴科技基础知识面试题(十三)

    2024-01-23 07:00:05       52 阅读
  10. 数据库系列文章之 ClickHouse入门

    2024-01-23 07:00:05       45 阅读
  11. 计算机网络(第六版)复习提纲7

    2024-01-23 07:00:05       61 阅读
  12. FFmpeg教程:libswscale对图像进行简单处理

    2024-01-23 07:00:05       59 阅读