Qt网络编程之QUdpSocket实例(三)

设想有如下场景:若干的客户端与服务器端建立连接,建立连接后,服务器端随机发送字符串给客户端,客户端打印输出。该节案例使用UDP编程。

服务器端

头文件

#pragma once
//////////////////////////////////////////////////////////////////////////
//UDP服务器端

#include <QObject>
class QUdpSocket;
class QTimer;
class SimpleUdpSocketServerDemo: public QObject
{
public:
	SimpleUdpSocketServerDemo();

private slots:
	void writeData();

private:
	QUdpSocket* m_pUdpSocket;
	QTimer* m_pTimer;
	int m_nMessageNo;
};

void testSimpleUdpSocketServerDemo();

源文件

#include "SimpleUdpSocketServerDemo.h"
#include <QDebug>
#include <QTimer>
#include <QUdpSocket>

SimpleUdpSocketServerDemo::SimpleUdpSocketServerDemo()
:m_nMessageNo(0)
{
	//1. 创建UDP套接字
	m_pUdpSocket = new QUdpSocket(this);
	//2. 创建定时器,每隔1s写数据包
	m_pTimer = new QTimer(this);
	m_pTimer->setInterval(1000);
	connect(m_pTimer, &QTimer::timeout, this, &SimpleUdpSocketServerDemo::writeData);
	//3. 开启定期器
	m_pTimer->start();
}

void SimpleUdpSocketServerDemo::writeData()
{
	//填写数据包内容
	QByteArray datagram = "Broadcast message " + QByteArray::number(m_nMessageNo);
	m_pUdpSocket->writeDatagram(datagram.data(), datagram.size(), QHostAddress::Broadcast, 45454);
	++m_nMessageNo;
	qDebug() << "SimpleUdpSocketServerDemo::writeData" << datagram;
}


void testSimpleUdpSocketServerDemo()
{
	//这样写会内存泄漏,如此写方便测试。
	SimpleUdpSocketServerDemo* pUdpSocketServerDemo = new SimpleUdpSocketServerDemo;
}

客户端

头文件

#pragma once
//////////////////////////////////////////////////////////////////////////
//udp客户端
#include <QObject>
#include <QRunnable>
#include <QThreadPool>

class QUdpSocket;
class SimpleUdpSocketClientDemo : public QObject
{
	Q_OBJECT
public:
	SimpleUdpSocketClientDemo();

private slots:
	void readPendingDatagrams();

private:
	QUdpSocket* m_pUdpSocket;
};

void testSimpleUdpSocketClientDemo();

源文件

#include "SimpleUdpSocketClientDemo.h"
#include <QDebug>
#include <QTime>
#include <QUdpSocket>

SimpleUdpSocketClientDemo::SimpleUdpSocketClientDemo()
{
	//1. 创建UDP套接字对象
	m_pUdpSocket = new QUdpSocket(this);
	m_pUdpSocket->bind(45454, QUdpSocket::ShareAddress);
	//2. 数据可读信号连接
	connect(m_pUdpSocket, &QUdpSocket::readyRead, this, &SimpleUdpSocketClientDemo::readPendingDatagrams); 
}

void SimpleUdpSocketClientDemo::readPendingDatagrams()
{
	while (m_pUdpSocket->hasPendingDatagrams())
	{
		//读数据包
		QByteArray oDatagram;
		oDatagram.resize(m_pUdpSocket->pendingDatagramSize());
		m_pUdpSocket->readDatagram(oDatagram.data(), oDatagram.size());
		qDebug() << " SimpleUdpSocketClientDemo::readPendingDatagrams" << oDatagram;
	}
}

void testSimpleUdpSocketClientDemo()
{
	//这样写会内存泄漏,如此写方便测试。
	SimpleUdpSocketClientDemo* pUdpSocketClientDemo = new SimpleUdpSocketClientDemo;
}

测试结果

服务器端

SimpleUdpSocketServerDemo::writeData "Broadcast message 4"
SimpleUdpSocketServerDemo::writeData "Broadcast message 5"
SimpleUdpSocketServerDemo::writeData "Broadcast message 6"
SimpleUdpSocketServerDemo::writeData "Broadcast message 7"
SimpleUdpSocketServerDemo::writeData "Broadcast message 8"
SimpleUdpSocketServerDemo::writeData "Broadcast message 9"
········

客户端

 SimpleUdpSocketClientDemo::readPendingDatagrams "Broadcast message 4"
 SimpleUdpSocketClientDemo::readPendingDatagrams "Broadcast message 5"
 SimpleUdpSocketClientDemo::readPendingDatagrams "Broadcast message 6"
 SimpleUdpSocketClientDemo::readPendingDatagrams "Broadcast message 7"
 SimpleUdpSocketClientDemo::readPendingDatagrams "Broadcast message 8"
 SimpleUdpSocketClientDemo::readPendingDatagrams "Broadcast message 9"
 ········

通过本案例的代码实现可以了解UDP服务器端/客户端编程的基本思路。

发布了26 篇原创文章 · 获赞 20 · 访问量 3万+
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 编程工作室 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览