Qt实现UDP通信的示例代码

目录
  • 服务器端-单线程
  • 客户端
  • 测试结果-单线程
  • 服务器端-多线程
  • 测试结果-多线程

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

服务器端-单线程

头文件

#pragma once
//
//tcp服务端-单线程处理客户端连接
#include <QAbstractSocket>
#include <QObject>

class QTcpServer;
class SimpleTcpSocketServerDemo : public QObject
{ 

    Q_OBJECT

public:
    SimpleTcpSocketServerDemo();

private slots:
    void sendData();
    void displayError(QAbstractSocket::SocketError socketError);

private:
    QStringList m_oData;
    QTcpServer *m_pTcpServer;
};

void testSimpleTcpSocketServerDemo();

源文件

#include "SimpleTcpSocketServerDemo.h"
#include <assert.h>
#include <QTcpServer>
#include <QTcpSocket>
#include <QDebug>
#include <QDataStream>
SimpleTcpSocketServerDemo::SimpleTcpSocketServerDemo()
{ 

//初始换原始数据
m_oData << tr("You've been leading a dog's life. Stay off the furniture.")
<< tr("You've got to think about tomorrow.")
<< tr("You will be surprised by a loud noise.")
<< tr("You will feel hungry again in another hour.")
<< tr("You might have mail.")
<< tr("You cannot kill time without injuring eternity.")
<< tr("Computers are not intelligent. They only think they are.");
//1. 创建TCP对象
m_pTcpServer = new QTcpServer(this);
//2. 新连接、错误信号
connect(m_pTcpServer, &QTcpServer::newConnection, this, &SimpleTcpSocketServerDemo::sendData);
connect(m_pTcpServer, &QTcpServer::acceptError, this, &SimpleTcpSocketServerDemo::displayError);
//3. 启动服务端
if (!m_pTcpServer->listen(QHostAddress::Any, 8888))
{ 

qDebug() << "m_pTcpServer->listen() error";
assert(false);
}
}
void SimpleTcpSocketServerDemo::sendData()
{ 

//获取服务端数据
QString sWriteData = m_oData.at(qrand() % m_oData.size());
//获取与客户端通信的socket
QTcpSocket* pClientConnection = m_pTcpServer->nextPendingConnection();
//从客户端读数据
QString sReadData = pClientConnection->readAll();
qDebug() << "SimpleTcpSocketServerDemo::readDataFromClient " << pClientConnection;
//与客户端写数据
qDebug() << "SimpleTcpSocketServerDemo::writeDataToClient " << sWriteData;
pClientConnection->write(sWriteData.toUtf8());
// //与客户端断开连接
// connect(pClientConnection, &QTcpSocket::disconnected, this, &SimpleTcpSocketServerDemo::deleteLater);
// pClientConnection->disconnectFromHost();
}
void SimpleTcpSocketServerDemo::displayError(QAbstractSocket::SocketError socketError)
{ 

qDebug() << "SimpleTcpSocketServerDemo::displayError " << socketError;
}
void testSimpleTcpSocketServerDemo()
{ 

//这样写会内存泄漏,如此写方便测试。
SimpleTcpSocketServerDemo* pSimpleTcpSocketServer = new SimpleTcpSocketServerDemo;
}

客户端

头文件

#pragma once
//
//客户端
#include <QObject>
#include <QAbstractSocket>
#include <QRunnable>
#include <QThreadPool>
class QTcpSocket;
class SimpleTcpSocketClientDemo : public QObject
{ 

Q_OBJECT
public:
SimpleTcpSocketClientDemo();
private slots:
void connected();
void readyRead();
void error(QAbstractSocket::SocketError socketError);
private:
QTcpSocket* m_pTcpSocket;
};
class ClientRunnable : public QRunnable
{ 

public:
void run();
};
void testSimpleTcpSocketClientDemo();

源文件

#include "SimpleTcpSocketClientDemo.h"
#include <QTcpSocket>
#include <QDebug>
SimpleTcpSocketClientDemo::SimpleTcpSocketClientDemo()
{ 

//1. 创建TCP套接字对象
m_pTcpSocket = new QTcpSocket(this);
//2. 已连接、数据可读、失败信号连接
connect(m_pTcpSocket, &QTcpSocket::connected, this, &SimpleTcpSocketClientDemo::connected);
connect(m_pTcpSocket, &QIODevice::readyRead, this, &SimpleTcpSocketClientDemo::readyRead);
typedef void (QAbstractSocket::*QAbstractSocketErrorSignal)(QAbstractSocket::SocketError);
connect(m_pTcpSocket, static_cast<QAbstractSocketErrorSignal>(&QTcpSocket::error), this, &SimpleTcpSocketClientDemo::error);
//3. 与服务器端建立连接
m_pTcpSocket->connectToHost("127.0.0.1", 8888);
//4. 同步处理-等待数据可读
m_pTcpSocket->waitForReadyRead();
}
void SimpleTcpSocketClientDemo::readyRead()
{ 

qDebug() << "SimpleTcpSocketClientDemo::readyRead " << m_pTcpSocket->readAll();
}
void SimpleTcpSocketClientDemo::connected()
{ 

qDebug() << "SimpleTcpSocketClientDemo::connected successfully";
}
void SimpleTcpSocketClientDemo::error(QAbstractSocket::SocketError socketError)
{ 

qDebug() << "SimpleTcpSocketClientDemo::error " << socketError;
}
void ClientRunnable::run()
{ 

//这样写会内存泄漏,如此写方便测试。
SimpleTcpSocketClientDemo* pSimpleTcpSocketClient = new SimpleTcpSocketClientDemo;
}
#define CLINET_COUNT 2000 //客户端的数量
void testSimpleTcpSocketClientDemo()
{ 

QTime oTime;
oTime.start();
//同步线程池的方式模拟多个客户端与服务器端交互
for (int nIndex = 0; nIndex < CLINET_COUNT; ++nIndex)
{ 

ClientRunnable* pRunnable = new ClientRunnable;
pRunnable->setAutoDelete(false);
QThreadPool::globalInstance()->start(pRunnable);
}
QThreadPool::globalInstance()->waitForDone(30 * 1000);
qDebug() << "connect count: " << CLINET_COUNT << "total time: " << (double)oTime.elapsed() / double(1000) << "s";
}

测试结果-单线程

服务器端

SimpleTcpSocketServerDemo::readDataFromClient  QTcpSocket(0x2f27f308)
SimpleTcpSocketServerDemo::writeDataToClient  "You will feel hungry again in another hour."
SimpleTcpSocketServerDemo::readDataFromClient  QTcpSocket(0x2eb61cf0)
SimpleTcpSocketServerDemo::writeDataToClient  "You might have mail."
.........

客户端

SimpleTcpSocketClientDemo::connected  successfully
SimpleTcpSocketClientDemo::readyRead  "You might have mail."
SimpleTcpSocketClientDemo::connected  successfully
SimpleTcpSocketClientDemo::readyRead  "You will feel hungry again in another hour."
.........
connect count:  2000 total time:  3.926 s

通过测试输出,可以看到服务器端与客户端建立了正确的连接并且数据交换。

– 实际测试数据:2000个连接,耗时4s左右,CPU使用率10%左右。

通过阅读服务器端,发现单线程处理客户端的连接效率较低。服务器端可修改为多线程处理客户端连接,代码如下:

服务器端-多线程

头文件

#pragma once
//
//服务器端-多线程处理客户端连接
#include <QTcpServer>
#include <QThread>
class MultiThreadTcpSocketServerDemo : public QTcpServer
{ 

public:
MultiThreadTcpSocketServerDemo();
//This virtual function is called by QTcpServer when a new connection is available.
//The socketDescriptor argument is the native socket descriptor for the accepted connection.
virtual void incomingConnection(qintptr handle);
private:
QStringList m_oData;
};
//处理线程
class ServerHandleThread : public QThread
{ 

Q_OBJECT
public:
ServerHandleThread(qintptr handle, const QString& sWriteData);
virtual void run();
private:
qintptr m_nHandle;
QString m_sWriteData;
};
void testMultiThreadTcpSocketServerDemo();

//This virtual function is called by QTcpServer when a new connection is available. //The socketDescriptor argument is the native socket descriptor for the accepted connection. virtual void incomingConnection(qintptr handle); //该虚函数是重点

源文件

#include "MultiThreadTcpSocketServerDemo.h"
#include <QDebug>
#include <QTcpSocket>
MultiThreadTcpSocketServerDemo::MultiThreadTcpSocketServerDemo()
{ 

//初始换原始数据
m_oData << tr("You've been leading a dog's life. Stay off the furniture.")
<< tr("You've got to think about tomorrow.")
<< tr("You will be surprised by a loud noise.")
<< tr("You will feel hungry again in another hour.")
<< tr("You might have mail.")
<< tr("You cannot kill time without injuring eternity.")
<< tr("Computers are not intelligent. They only think they are.");
}
void MultiThreadTcpSocketServerDemo::incomingConnection(qintptr handle)
{ 

//获取服务端数据
QString sWriteData = m_oData.at(qrand() % m_oData.size());
qDebug() << "MultiThreadTcpSocketServerDemo::incomingConnection" << handle;
ServerHandleThread* pThread = new ServerHandleThread(handle, sWriteData);
connect(pThread, &ServerHandleThread::finished, pThread, &ServerHandleThread::deleteLater);
pThread->start();
}
ServerHandleThread::ServerHandleThread(qintptr handle, const QString& sWriteData)
:m_sWriteData(sWriteData), m_nHandle(handle)
{ 

}
void ServerHandleThread::run()
{ 

//1. 建立与客户端通信的TCP套接字
QTcpSocket oTcpSocket;
if (!oTcpSocket.setSocketDescriptor(m_nHandle))
{ 

qDebug() << "oTcpSocket.setSocketDescriptor error";
return;
}
//2. 向客户端写数据
qDebug() << "MultiThreadTcpSocketServerDemo::readDataFromClient" << &oTcpSocket;
qDebug() << "MultiThreadTcpSocketServerDemo::writeDataToClient" << m_sWriteData;
oTcpSocket.write(m_sWriteData.toUtf8());
oTcpSocket.disconnectFromHost();
oTcpSocket.waitForDisconnected();
}
void testMultiThreadTcpSocketServerDemo()
{ 

//1. 建立服务器端套接字
MultiThreadTcpSocketServerDemo* m_pTcpServer = new MultiThreadTcpSocketServerDemo();
//2. 启动服务端
if (!m_pTcpServer->listen(QHostAddress::Any, 8888))
{ 

qDebug() << "m_pTcpServer->listen() error";
}
}

测试结果-多线程

客户端

SimpleTcpSocketClientDemo::connected  successfully
SimpleTcpSocketClientDemo::readyRead  "You might have mail."
SimpleTcpSocketClientDemo::connected  successfully
SimpleTcpSocketClientDemo::readyRead  "You will feel hungry again in another hour."
.........
connect count:  2000 total time:  6.403 s

– 实际测试数据:2000个连接,耗时6.5s左右,CPU使用率20%左右。

可见服务器端采用多线程可充分利用CPU,但是频繁的切换线程也会性能下降(耗时)。

通过本案例的代码实现可以了解TCP服务器端/客户端编程的基本思路。并且验证了服务器端单线程和多线程的效率对比。 在windows中,可通过IOCP提高服务期端的效率,后面会详细讲解。

到此这篇关于Qt实现UDP通信的示例代码的文章就介绍到这了,更多相关Qt UDP通信内容请搜索我们以前的文章或继续浏览下面的相关文章希望大家以后多多支持我们!

(0)

相关推荐

  • Qt实现简单UDP通信

    Qt简单实现UDP通信的具体代码,供大家参考,具体内容如下 标签(空格分隔): udp通信.Qt 一.UDP简介 1)UDP(User Datagram Protocol,用户数据报协议) UDP是一个轻量级.不可靠.面向数据报的.无连接的传输层协议,多用于可靠性要求不严格,不是非常重要的传输,如直播.视频会议等等. 2)Qt中QUdpSocket类继承自QAbstractSocket,用来发送和接收UDP数据报,”Socket”即套接字,套接字即IP地址+端口号.其中IP地址指定了网络中的一台

  • QT网络编程UDP下C/S架构广播通信(实例讲解)

    QT有封装好的UDP协议的类,QUdpSocket,里面有我们想要的函数接口.感兴趣的话,可以看看. 先搞服务端吧,写一个子类,继承QDialog类,起名为UdpServer类.头文件要引用我们上边说的QUdpSocket这个类,还有我们想要的布局的类. #ifndef UDPSERVER_H #define UDPSERVER_H #include <QDialog> #include <QLabel> #include <QLineEdit> #include &l

  • QT5实现UDP通信的示例代码

    目录 前言 一.UDP通信概述 二.UDP单播模式 1.接收数据 2.发送数据 总结 代码h文件 代码c文件 前言 该例程经过实际验证可以正常使用,只简单的使用UDP中的单播模式(一对一),其余模式将在后期逐步说明...... 所用测试系统在同一局域网,其中: QT版本:5.12 PC端UDP模式:单播 UDP通信目标:基于STM32F4+LWIP协议的以太网接口 一.UDP通信概述 UDP是轻量的.不可靠的.面向数据报.无连接的协议,它可以用于对可靠性要求不高的场合,和TCP通信不同,两个程序

  • Qt实现UDP通信的示例代码

    目录 服务器端-单线程 客户端 测试结果-单线程 服务器端-多线程 测试结果-多线程 设想有如下场景:若干的客户端与服务器端建立连接,建立连接后,服务器端随机发送字符串给客户端,客户端打印输出.该节案例使用TCP编程. 服务器端-单线程 头文件 #pragma once // //tcp服务端-单线程处理客户端连接 #include <QAbstractSocket> #include <QObject> class QTcpServer; class SimpleTcpSocke

  • python实现一个简单的udp通信的示例代码

    什么是 Socket? Socket又称"套接字",应用程序通常通过"套接字"向网络发出请求或者应答网络请求,使主机间或者一台计算机上的进程间可以通讯. python创建套接字 socket.socket([family[, type[, proto]]]) 参数解析: family: AF_UNIX(适用跨机通信) 或 AF_INET(IPv4)(适用本机通信) type: 套接字类型,可以根据是面向连接的还是非连接分为 SOCK_STREAM(TCP) 或 SO

  • C# 使用Tcp/Udp协议的示例代码

    所使用的:多线程 委托 Socket 键值队 个人跟着视频做的一个小练习,有兴趣的可以去看一下对于初学者来讲是比较有帮助的 连接:https://www.bilibili.com/video/BV1bZ4y1W74q?p=3&t=358 废话不多说上代码  综合视频中的理解 有什么不对的地方希望大神指点一下 public partial class Form1 : Form { public Form1() { InitializeComponent(); myAddOnlieDelegate

  • C#中使用UDP通信的示例

    网络通信协议中的UDP通信是无连接通信,客户端在发送数据前无需与服务器端建立连接,即使服务器端不在线也可以发送,但是不能保证服务器端可以收到数据.本文实例即为基于C#实现的UDP通信.具体功能代码如下: 服务器端代码如下 static void Main(string[] args) {   UdpClient client = null;   string receiveString = null;   byte[] receiveData = null;   //实例化一个远程端点,IP和端

  • python实现socket简单通信的示例代码

    首先先来简单介绍下socket: (具体更详细介绍的可以在网上找找,都讲得非常详细),这里主要是我自己的一些理解. socket是在应用层与传输层之间的一个抽象层,它的本质是编程接口,通过socket,才能实现TCP/IP协议. 它就是一个底层套件,用来处理最底层消息的接受和发送. socket翻译为套接字,可以把TCP/IP复杂的操作抽象为简单的几个接口来供应用层调用来实现进程在网络中的通信.socket起源于Unix,而Unix的基本要素之一就是"一切都为文件",即可以通过打开--

  • React Native JSI实现RN与原生通信的示例代码

    目录 什么是JSI JSI有什么不同 在iOS中使用JSI iOS端配置 RN端配置 js调用带参数的原生方法 原生调用JS 原生调用带参数的JS方法 在原生端调用js的函数参数 总结 问题 参考资料 什么是JSI React Native JSI (JavaScript Interface) 可以使 JavaScript 和 原生模块 更快.更简单的通信.它也是React Native 新的架构体系中Fabric UI层 和 Turbo 模块的核心部分. JSI有什么不同 JSI 移除了原生代

  • C/C++ QT实现自定义对话框的示例代码

    对话框分为多种,常见的有通用对话框,自定义对话框,模态对话框,非模态对话框等,其中通用对话框包括了,QFileDialog文件对话框,QColorDialog颜色对话框,QFontDialog字体对话框,QInputDialog输入对话框等,自定义对话框则主要是实现自己布局的简单页面,区别于窗体对话框则显得更加简单一些,除对话框外,多窗体设计也是最常用的,例如多窗体嵌入,MID窗体等,下面则是每种窗体的代码总结. 创建自定义窗体 1.首先使用两个控件,TableView主要是表格处理,TreeV

  • C++ 实现即时通信的示例代码(直接运行)

    目录 题目 软件:VS 服务器端 客户端 题目 由于本学期上了网络编程课程,老师要求写使用Socke实现网络编程.于是参考C++多线程实现即时通信软件写出了简单版本的没有界面的即时通信软件. 软件:VS 直接上代码,需要讲解原理的,可以参考C++多线程实现即时通信软件 服务器端 //TcpServer_plus.exe #include<stdio.h> #include <Winsock2.h> #include<WS2tcpip.h> #pragma comment

  • Qt创建SQlite数据库的示例代码

    Qt 创建 SQlite数据库 void Widget::initDB() { // 创建并打开数据库 QSqlDatabase database; database = QSqlDatabase::addDatabase("QSQLITE"); // qDebug() << QApplication::applicationDirPath(); // 获取应用程序当前目录 database.setDatabaseName("test.sqlite3")

随机推荐