用户数据报协议是一种简单的轻量级、不可靠、面向数据、无连接的传出层协议,可以应用于在可靠性不是十分重要的场合,如短消息,广播信息等。

例如一下场合

网络数据大多为短消息

拥有大量客户端

对数据安全性无特殊要求

网络负担飞常重,但对响应速度要求高

示例截图

服务器代码

.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网络通讯的更多相关文章

  1. 网络协议之UDP

    前言 TCP协议在不可靠的网络环境上提供了可靠的通信通道,隐藏了大量的底层细节,使应用程序更加简洁.但有些应用并不需要这么高的可靠性,并不需要按序交付,而且TCP为了提高可靠性也增加了延时,在某些对延 ...

  2. UDP网络程序模型设计

    UDP网络程序设计 1. UDP网络编程模型程序初始化 1.1服务器使用的函数 创建socket----->socket 绑定地址-------->bind 接受数据--------> ...

  3. 37.Qt网络与通信

    1 获取本机网络与通信 在网络应用中,经常需要获得本机的主机名.IP地址和硬件地址等网络信息.运用QHostInfo,QNetWorkInterface,QNetworkAddressEntry可获得 ...

  4. 网络编程 单纯UDP通信

    网络编程 单纯UDP通信 1,UDP发送端 2,UDP接收端 UDP发送端: #include <stdio.h> #include <unistd.h> #include & ...

  5. 网络通信协议、UDP通信、TCP通信

    网络通信协议 网络通信协议有很多种,目前应用最广泛的是TCP/IP协议,它是一个包括TCP协议和IP协议,UDP协议和其它一些协议的协议组. IP地址和端口号 目前,IP地址广泛使用的版本是IPv4, ...

  6. python网络-Socket之udp编程(24)

    一.udp简介 udp --- 用户数据报协议,是一个无连接的简单的面向数据报的运输层协议. udp不提供可靠性,它只是把应用程序传给IP层的数据报发送出去,但是并不能保证它们能到达目的地. udp在 ...

  7. TCP/IP协议网络编程以及UDP和TCP之传输协议

    1.什么是TCP/IP协议? 网络编程协议有很多,目前应用最广泛的是TCP/IP协议(Transmission Control Protocal/Internet Protoal 传输控制协议/英特网 ...

  8. Raknet是一个基于UDP网络传输协议的C++网络库(还有一些其它库,比如nanomsg,fastsocket等等)

    Raknet是一个基于UDP网络传输协议的C++网络库,允许程序员在他们自己的程序中实现高效的网络传输服务.通常情况下用于游戏,但也可以用于其它项目. Raknet有以下好处: 高性能 在同一台计算机 ...

  9. 分布式系统(二) --SOA 以及一些网络通信协议TCP/UDP SYN攻击

    SOA(面向服务的架构):Service Oriented Architecture面向服务的架构.也就是把工程拆分成服务层.表现层两个工程.服务层中包含业务逻辑,只需要对外提供服务即可.表现层只需要 ...

随机推荐

  1. ASP.NET Web API编程——文件上传

    首先分别介绍正确的做法和错误的做法,然后分析他们的不同和错误之处,以便读者在实现此功能时可避开误区 1正确的做法 public class AvaterController : BaseApiCont ...

  2. cmd进入指定的文件夹

    怎么利用cmd进入指定的文件夹呢? 1:win+r ——cmd 2:进入要到达的盘符 (比如我要进入d盘) 3:然后通过  cd d:\project    进入指定的文件夹

  3. 解决pycharm无法导入本地包的问题

    在用python写爬虫程序时,import 行无法通过,具体情况如下: pycharm运行程序后,程序pass了,但是出现了警告,如下图所示: 这是由于该程序不在根目录下,无法导入本地包,解决办法如下 ...

  4. NHibernate学习过程笔记

    NHbernate自动生成数据库的方法: using NHibernate; using NHibernate.Tool.hbm2ddl; namespace Test { public class ...

  5. EF Core如何输出日志到Visual Studio的输出窗口

    我们在使用EF Core的时候,很多时候需要在Visual Studio的输出窗口中知道EF Core在后台生成的SQL语句是什么,这个需求可以通过自定义EF Core的ILoggerFactory和 ...

  6. 给requests模块添加请求头列表和代理ip列表

    Requests 是使用 Apache2 Licensed 许可证的 基于Python开发的HTTP 库,其在Python内置模块的基础上进行了高度的封装,符合了Python语言的思想,通俗的说去繁存 ...

  7. window下安装composer

    1.什么是composer 一个智能的下载工具.比如说我的项目要安装yii框架,而yii是依赖于其他东西的,仅仅安装yii是不够的,这样会导致我的项目也不能正常运行:怎么办呢,我们可以一个一个手动的将 ...

  8. js中两个日期大小比较,获取当前日期,日期加减一天

    一.两个日期大小比较 1.日期参数格式:yyyy-mm-dd // a: 日期a, b: 日期b, flag: 返回的结果 function duibi(a, b,flag) { var arr = ...

  9. Crash for small compressed texture on some Android device

    I created a full white texture with 4x4 size. Unity requires that compressed texture size should be ...

  10. C++笔记008:C++对C的扩展——命名空间 namespace基础

    原创笔记,转载请注明出处! 点击[关注],关注也是一种美德~ 第一, 命名空间的意义 命名空间是ANSIC++引入的可以由用户命名的作用域,用来处理程序中常见的同名冲突. 我认识两位叫“A”的朋友,一 ...