Qt-网络与通信-UDP网络通讯
用户数据报协议是一种简单的轻量级、不可靠、面向数据、无连接的传出层协议,可以应用于在可靠性不是十分重要的场合,如短消息,广播信息等。
例如一下场合
网络数据大多为短消息
拥有大量客户端
对数据安全性无特殊要求
网络负担飞常重,但对响应速度要求高
示例截图
服务器代码
.h
#ifndef UDPSERVER_H
#define UDPSERVER_H #include <QDialog>
#include <QLabel>
#include <QLineEdit>
#include <QPushButton>
#include <QVBoxLayout>
#include <QUdpSocket>
#include <QTimer>
class UdpServer : public QDialog
{
Q_OBJECT public:
UdpServer(QWidget *parent = 0);
~UdpServer(); public slots:
void StartBtnClicked();
void timeout();
private:
QLabel *TimerLabel;
QLineEdit *TextLineEdit;
QPushButton *StartBtn;
QVBoxLayout *mainLayout; int port;
bool isStarted;
QUdpSocket *udpSocket;
QTimer *timer; }; #endif // UDPSERVER_H
.cpp
#include "udpserver.h" UdpServer::UdpServer(QWidget *parent)
: QDialog(parent)
{
setWindowTitle(tr("UDP Server"));
TimerLabel = new QLabel(tr("计时器:"),this);
TextLineEdit = new QLineEdit(this);
StartBtn = new QPushButton(tr("Start"),this); mainLayout = new QVBoxLayout(this);
mainLayout->addWidget(TimerLabel);
mainLayout->addWidget(TextLineEdit);
mainLayout->addWidget(StartBtn); connect(StartBtn,SIGNAL(clicked(bool)),this,SLOT(StartBtnClicked()));
port = 5555;
isStarted = false;
udpSocket = new QUdpSocket(this);
timer = new QTimer(this); connect(timer,SIGNAL(timeout()),this,SLOT(timeout()));
} UdpServer::~UdpServer()
{ } void UdpServer::StartBtnClicked()
{
if(!isStarted)
{
StartBtn->setText("Stop");
timer->start(1000);
isStarted = true;
}
else
{
StartBtn->setText("Start");
isStarted = false;
timer->stop();
}
} void UdpServer::timeout()
{ QString msg = TextLineEdit->text();
int length = 0; if(msg =="")
{
return;
}
if((length = udpSocket->writeDatagram(msg.toLatin1(),msg.length(),QHostAddress::Broadcast,port)) != msg.length())
{
return;
}
}
客户端代码
.h
#ifndef UDPCLIENT_H
#define UDPCLIENT_H #include <QDialog>
#include <QVBoxLayout>
#include <QTextEdit>
#include <QPushButton>
#include <QUdpSocket>
class UdpClient : public QDialog
{
Q_OBJECT public:
UdpClient(QWidget *parent = 0);
~UdpClient();
public slots:
void CloseBtnClicked();
void dataReceived();
private:
QTextEdit *ReceiveTextEdit;
QPushButton *CloseBtn;
QVBoxLayout *mainLayout; int port;
QUdpSocket *udpSocket;
}; #endif // UDPCLIENT_H
.cpp
#include "udpclient.h"
#include <QMessageBox>
#include <QHostAddress>
UdpClient::UdpClient(QWidget *parent)
: QDialog(parent)
{
setWindowTitle("UODClient");
ReceiveTextEdit = new QTextEdit(this);
CloseBtn = new QPushButton("close",this); mainLayout = new QVBoxLayout(this); mainLayout->addWidget(ReceiveTextEdit);
mainLayout->addWidget(CloseBtn); connect(CloseBtn,SIGNAL(clicked(bool)),this,SLOT(CloseBtnClicked()));
port = 5555;
udpSocket = new QUdpSocket(this); connect(udpSocket,SIGNAL(readyRead()),this,SLOT(dataReceived())); bool result = udpSocket->bind(port);
if(!result)
{
QMessageBox::information(this,"Error","udp socket create error!");
return;
}
} UdpClient::~UdpClient()
{ } void UdpClient::CloseBtnClicked()
{
close();
} void UdpClient::dataReceived()
{
while (udpSocket->hasPendingDatagrams())
{
QByteArray datagram;
datagram.resize(udpSocket->pendingDatagramSize());
udpSocket->readDatagram(datagram.data(),datagram.size());
QString msg = datagram.data();
ReceiveTextEdit->insertPlainText(msg);
} }
工程连接:https://gitee.com/DreamLife-Technology_DreamLife/UDPProject
Qt-网络与通信-UDP网络通讯的更多相关文章
- 网络协议之UDP
前言 TCP协议在不可靠的网络环境上提供了可靠的通信通道,隐藏了大量的底层细节,使应用程序更加简洁.但有些应用并不需要这么高的可靠性,并不需要按序交付,而且TCP为了提高可靠性也增加了延时,在某些对延 ...
- UDP网络程序模型设计
UDP网络程序设计 1. UDP网络编程模型程序初始化 1.1服务器使用的函数 创建socket----->socket 绑定地址-------->bind 接受数据--------> ...
- 37.Qt网络与通信
1 获取本机网络与通信 在网络应用中,经常需要获得本机的主机名.IP地址和硬件地址等网络信息.运用QHostInfo,QNetWorkInterface,QNetworkAddressEntry可获得 ...
- 网络编程 单纯UDP通信
网络编程 单纯UDP通信 1,UDP发送端 2,UDP接收端 UDP发送端: #include <stdio.h> #include <unistd.h> #include & ...
- 网络通信协议、UDP通信、TCP通信
网络通信协议 网络通信协议有很多种,目前应用最广泛的是TCP/IP协议,它是一个包括TCP协议和IP协议,UDP协议和其它一些协议的协议组. IP地址和端口号 目前,IP地址广泛使用的版本是IPv4, ...
- python网络-Socket之udp编程(24)
一.udp简介 udp --- 用户数据报协议,是一个无连接的简单的面向数据报的运输层协议. udp不提供可靠性,它只是把应用程序传给IP层的数据报发送出去,但是并不能保证它们能到达目的地. udp在 ...
- TCP/IP协议网络编程以及UDP和TCP之传输协议
1.什么是TCP/IP协议? 网络编程协议有很多,目前应用最广泛的是TCP/IP协议(Transmission Control Protocal/Internet Protoal 传输控制协议/英特网 ...
- Raknet是一个基于UDP网络传输协议的C++网络库(还有一些其它库,比如nanomsg,fastsocket等等)
Raknet是一个基于UDP网络传输协议的C++网络库,允许程序员在他们自己的程序中实现高效的网络传输服务.通常情况下用于游戏,但也可以用于其它项目. Raknet有以下好处: 高性能 在同一台计算机 ...
- 分布式系统(二) --SOA 以及一些网络通信协议TCP/UDP SYN攻击
SOA(面向服务的架构):Service Oriented Architecture面向服务的架构.也就是把工程拆分成服务层.表现层两个工程.服务层中包含业务逻辑,只需要对外提供服务即可.表现层只需要 ...
随机推荐
- Leetcode225 用栈实现队列
大众思路: 用两个栈实现,记为s1,s2 1.元素入栈时,加入s1 2.元素出栈时,对s2进行判断,如果s2为空,则将全部s1元素弹出并压入到s2,然后从s2栈顶弹出一个元素:如果s2不为空,则直接从 ...
- PAT——1005. 继续(3n+1)猜想 (25)
卡拉兹(Callatz)猜想已经在1001中给出了描述.在这个题目里,情况稍微有些复杂. 当我们验证卡拉兹猜想的时候,为了避免重复计算,可以记录下递推过程中遇到的每一个数.例如对n=3进行验证的时候, ...
- [LCOI2018][WX] Tirpitz
\([LCOI2018][WX11.1]~Tirpitz\) 时限:1s 内存限制:131072KB 输入文件: T.in 输出文件: T.out 题目背景 王九日很颓废,这也就是他为什么这么弱的原 ...
- ARC声明属性关键字详解(strong,weak,unsafe_unretained,copy)
ARC声明属性关键字详解(strong,weak,unsafe_unretained,copy) 在iOS开发过程中,属性的定义往往与retain, assign, copy有关,我想大家都很熟悉了, ...
- 第21章 DMA—直接存储区访问
本章参考资料:<STM32F76xxx参考手册>DMA控制器章节. 学习本章时,配合<STM32F76xxx参考手册>DMA控制器章节一起阅读,效果会更佳,特别是涉及到寄存器说 ...
- Vim插件及.vimrc文件的配置
关于Vim的配置基本是在.vimrc文件中完成的,该文件一般放在用户目录下. 今天安装了插件管理器Pathogen和python自动补全的插件pydiction,其中Pathogen需要事先创建两个文 ...
- UE4 Navmesh 室内导航设置
我用的UE版本是4.14.1 系统:win10 64 前不久给样板房里面做了一个扫地机器人,导航设置让我头大了很久,度娘也没有用,最后在谷哥上有所感悟,现在给出本人的设置过程和解决方案. 一开始拖 ...
- Oracle中case的第二种用法
procedure P_GetProVerSingInfo_2018(varFileID in varchar2, p_cr1 out refcontent, p_cr2 out refcontent ...
- input和div模仿select,带输入提示
有时候我们需要select和input的结合体,即可以使用下拉框,同时也可以用来输入,输入的同时显示可选的下拉选项 先上html代码 <div class="input-group i ...
- Spring注解配置(1)——@Autowired
@Autowired 注释,它可以对类成员变量.方法及构造函数进行标注,完成自动装配的工作. 通过 @Autowired的使用来消除 set ,get方法.在使用@Autowired之前,我们对一个b ...