Qt-网络与通信-UDP网络通讯
2018-02-28 21:54
597 查看
用户数据报协议是一种简单的轻量级、不可靠、面向数据、无连接的传出层协议,可以应用于在可靠性不是十分重要的场合,如短消息,广播信息等。
例如一下场合
网络数据大多为短消息
拥有大量客户端
对数据安全性无特殊要求
网络负担飞常重,但对响应速度要求高
示例截图
服务器代码
.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
例如一下场合
网络数据大多为短消息
拥有大量客户端
对数据安全性无特殊要求
网络负担飞常重,但对响应速度要求高
示例截图
服务器代码
.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 简单的网络通信(tcp ,udp)
- qt5学习之网络通讯(qt5 udp 客户端和服务端程序)
- Qt开发:TCP和UDP网络通信
- unity开发:Qt C++与unity之间UDP网络通信
- 网络编程-udp通信
- TCP和UDP网络通讯的区别及实现方式
- 08-03 网络通信(UDP) URL XML解析器 JSON、DOM、SAX 枚举
- 网络通讯方式 ------ TCP、UDP
- 用java编写简单UDP网络通信程序
- 5.关于QT中的网络编程,QTcpSocket,QUdpSocket
- linux 网络编程:客户端与服务器通过TCP协议相互通信 + UDP
- [QT]QT教程之实例分析[九] 网络广播程序(QUdpSocket)
- Android网络开发中如何使用JSON进行网络通信---Android JSON数据通讯方法解析
- TCP和UDP网络通讯的区别及实现方式
- Java网络编程之简单UDP通信
- Java系列讲座二:Socket网络通信实现聊天软件项目讲解(UDP)
- 【C/C++学院】(20)QT:udp通信
- ios调用unix 网络编程的socket 接口实行UDP通信, 锁屏后解屏会闪退的解决方法
- 13.进程间通信---网络通信(TCP协议和UDP协议的比较)
- Android 网络通讯、通信