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

例如一下场合

网络数据大多为短消息

拥有大量客户端

对数据安全性无特殊要求

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

示例截图

服务器代码

.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. BZOJ 1878 [SDOI2009]HH的项链 【莫队】

    任意门:https://www.lydsy.com/JudgeOnline/problem.php?id=1878 1878: [SDOI2009]HH的项链 Time Limit: 4 Sec  M ...

  2. 理解JavaScript数据类型

    JavaScript有5种基本数据类型: 数值(number):整数和小数(比如1和3.14) 字符串(string):字符组成的文本(比如"Hello World") 布尔值(b ...

  3. Spring 4 mvc+shiro+thymeleaf+JPA(Hibernate)+MySql eclipse项目模板

    本模板基本配制为:spring 4.3.8+thymeleaf 3.0.3 +hibernate 5.5.5 + mysql 5.7 IDE:eclipse 运行环境为:Tomcat 8.0.28 项 ...

  4. jQuery序列化Ajax提交表单

    var formData=$("form").serialize(); $.ajax({ type: "POST", url: "/front/EPt ...

  5. get-pip.py 安装

    http://www.pip-installer.org/en/latest/installing.html$ curl http://pypi.python.org/packages/source/ ...

  6. Google File System设计方面的问题汇总

    1.Google File System概述 google file system是一个分布式文件系统,针对的是数据密集型应用,提供容错功能,运行在低廉的服务器上,同时给大量的用户提供高性能服务.尽管 ...

  7. redis主从架构的搭建

    本项目采用主从架构,一主两从一个哨兵.在x.x.x.69上部署主节点,在70上部署从节点1和哨兵节点,在71上部署从节点2. 准备: 1.首先上传redis文件到三台linux上,目录/home/sy ...

  8. SQLMAP使用详解

    使用示例 python sqlmap.py -u "http://xx.com/member.php?id=XX"  -p id --dbms "Mysql"  ...

  9. 从技术上分析八叉网www.xxxxxxxxvideos.com的自动定时发布文章功能是怎么实现的

    做网站开发的都需要用到网站广告自动定时发布功能,也就是说,编辑在网站后台把文章编写好之后,设置发布时间,点确定后发布,这时在网站前台访客是看不到这篇文章的,必须要等到文章设置的发布时间之后才能看到.八 ...

  10. 如何利用Linux去油管下载带字幕的优质英文资料提升英文听力和词汇量

    非常方便地从油管下载你需要的任何英文视频资料,并且带字幕,方便你学习某个特定领域的词汇: [step1,Centos6系统安装youtbe-dl下载带英文字幕的视频] 1.首先需要安装youtube- ...