網站首頁 編程語言 正文
本文實例為大家分享了QT UDP實現簡單消息傳輸的具體代碼,供大家參考,具體內容如下
這幾天看了下Qt的udp,順便實現了下簡單的消息傳輸,看起來比較簡單。
UDP服務器:
截圖如下:
代碼:
server.h
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include<QUdpSocket>
#include<QtNetwork>
#include<QLabel>
#include<QPushButton>
#include<QLineEdit>
#include<QGridLayout>
#include<QTimer>
class Widget : public QWidget
{
? ? Q_OBJECT
public:
? ? Widget(QWidget *parent = 0);
? ? ~Widget();
protected:
? ? QTimer* timer;
? ? QImage* image;
private slots:
? ? void send();
? ? void bordcaststart();
private:
? ? QPushButton* start_ptn;
? ? QPushButton* close_ptn;
? ? QPushButton* send_ptn;
? ? QLabel* label;
? ? QLineEdit* edit;
? ? QLabel* image_label;
? ? QGridLayout* layout;
? ? QUdpSocket* udpsocket;
};
#endif // WIDGET_H
server.cpp
#include "widget.h"
Widget::Widget(QWidget *parent)
? ? : QWidget(parent)
{
? ? start_ptn=new QPushButton("start");
? ? close_ptn=new QPushButton("quit");
? ? send_ptn=new QPushButton("send");
? ? label=new QLabel;
? ? label->setText("this is test!");
? ? edit=new QLineEdit;
? ? layout=new QGridLayout(this);
? ? layout->addWidget(label);
? ? layout->addWidget(edit);
? ? layout->addWidget(send_ptn,1,1);
? ? layout->addWidget(start_ptn,2,0);
? ? layout->addWidget(close_ptn,2,1);
? ? this->resize(400,400);
? ? timer=new QTimer(this);
? ? udpsocket=new QUdpSocket(this);
? ? connect(start_ptn,SIGNAL(clicked(bool)),this,SLOT(bordcaststart()));
? ? connect(close_ptn,SIGNAL(clicked(bool)),this,SLOT(close()));
// ? ?connect(timer,SIGNAL(timeout()),this,SLOT(send()));
}
Widget::~Widget()
{
}
void Widget::send()
{
? ? QByteArray datagram= "Broadcast message " +edit->text().toUtf8();
? ? udpsocket->writeDatagram(datagram.data(),datagram.size(),QHostAddress::Broadcast,45454);
}
void Widget::bordcaststart()
{
? ?// timer->start(1000);
? ? start_ptn->setDisabled(true);
? ? connect(send_ptn,SIGNAL(clicked(bool)),this,SLOT(send()));
}
客戶端
截圖:
client.h
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include<QUdpSocket>
#include<QtNetwork>
#include<QLabel>
#include<QTimer>
#include<QPushButton>
#include<QGridLayout>
class Widget : public QWidget
{
? ? Q_OBJECT
public:
? ? Widget(QWidget *parent = 0);
? ? ~Widget();
protected:
private:
? ? QGridLayout* layout;
? ? QPushButton* quit_ptn;
? ? QLabel* label;
? ? QUdpSocket* udpsocket;
private slots:
? ? void boarcast();
};
#endif // WIDGET_H
client.cpp
#include "widget.h"
Widget::Widget(QWidget *parent)
? ? : QWidget(parent)
{
? ? label=new QLabel;
? ? quit_ptn=new QPushButton("quit");
? ? layout=new QGridLayout(this);
? ? layout->addWidget(label);
? ? layout->addWidget(quit_ptn);
? ? this->resize(200,200);
? ? udpsocket=new QUdpSocket(this);
? ? udpsocket->bind(45454, QUdpSocket::ShareAddress);
? ? connect(udpsocket,SIGNAL(readyRead()),this,SLOT(boarcast()));
? ? connect(quit_ptn,SIGNAL(clicked(bool)),this,SLOT(close()));
}
Widget::~Widget()
{
}
void Widget::boarcast()
{
? ? while (udpsocket->hasPendingDatagrams()) {
? ? ? ? ? ? QByteArray datagram;
? ? ? ? ? ? datagram.resize(udpsocket->pendingDatagramSize());
? ? ? ? ? ? QHostAddress sender;
? ? ? ? ? ? quint16 senderPort;
? ? ? ? ? ? udpsocket->readDatagram(datagram.data(), datagram.size(),
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? &sender, &senderPort);
? ? ? ? ? ? label->setText(datagram);
? ? }
}
原文鏈接:https://blog.csdn.net/u013812682/article/details/52149665
相關推薦
- 2023-07-24 navigator里包含tabBar內容時失效
- 2023-04-02 python導出requirements.txt的幾種方法總結_python
- 2021-12-13 C++ 之常量const(常對象、常數據成員、常成員函數)
- 2022-11-15 如何將Android?Studio卸載干凈_Android
- 2022-09-17 python?df遍歷的N種方式(小結)_python
- 2022-05-20 python?特有語法推導式的基本使用_python
- 2022-11-08 uni-app 使用escook/request-miniprogram插件發請求
- 2022-07-03 el-form表單新增表單項動態校驗;el-form校驗動態表單v-if不生效;
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支