qt tcpsocket_qt中udp通信

qt tcpsocket_qt中udp通信设想有如下场景:若干的客户端与服务器端建立连接,建立连接后,服务器端随机发送字符串给客户端,客户端打印输出。该节案例使用TCP编程。服务器端-单线程头文件#pragmaonce////////////////////////////////////////////////////////////////////////////tcp服务端-单线程处理客户端连接#include&lt…

大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。

Jetbrains全系列IDE使用 1年只要46元 售后保障 童叟无欺

设想有如下场景:若干的客户端与服务器端建立连接,建立连接后,服务器端随机发送字符串给客户端,客户端打印输出。该节案例使用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提高服务期端的效率,后面会详细讲解。

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/195619.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 缓冲区溢出漏洞_电脑基于堆栈的缓冲区溢出

    缓冲区溢出漏洞_电脑基于堆栈的缓冲区溢出缓冲区溢出缓冲区溢出是指当计算机向缓冲区内填充数据时超过了缓冲区本身的容量,溢出的数据覆盖在合法数据上。理想的情况是:程序检查数据长度并不允许输入超过缓冲区长度的字符,但是绝大多数程序都会假设数据长度总是与所分配的储存空间相匹配,这就为缓冲区溢出埋下了隐患。操作系统所使用的缓冲区,又被称为”堆栈”。在各个操作进程之间,指令会被临时储存在“堆栈”中,“堆栈”也会出现缓冲区溢出。缓冲区溢出的危害…

    2025年6月12日
    4
  • PID算法详解[通俗易懂]

    PID算法详解[通俗易懂]PID算法是一种具有预见性的控制算法,其核心思想是:1>.PID算法不但考虑控制对象的当前状态值(现在状态),而且还考虑控制对象过去一段时间的状态值(历史状态)和最近一段时间的状态值变化(预期),由这3方面共同决定当前的输出控制信号;2>.PID控制算法的运算结果是一个数,利用这个数来控制被控对象在多种工作状态(比如加热器的多种功率,阀门的多种开度等)工作,一般输出形式为PWM,基本上满足了按需输出控制信号,根据情况随时改变输出的目的。比例控制是一种最简单的控制方式。其控制器的输出与输入误差信号成比例

    2022年9月26日
    0
  • linux指令popd,Linux切换目录pushd、popd、dirs命令

    linux指令popd,Linux切换目录pushd、popd、dirs命令Linux 切换目录 pushd popd dirs 命令 pushd 和 popd 在 linux 中可以用来方便地在多个目录之间切换 切换目录虽然有一点复杂 在实际中 发现通过使用 pushd 和 popd 能够极大地提高效率 cd 表示返回当前用户的默认目录 cd 表示回到之前的目录 cqs centos7share pushd boot grub boot grub usr share data

    2025年11月29日
    2
  • 【机器学习中的数学】多项式分布及其共轭分布

    【机器学习中的数学】多项式分布及其共轭分布多项变量(MultinomialVariables)二元变量是用来描述只有两种可能值的量,而当我们遇到一种离散变量,其可以有K种可能的状态。我们可以使用一个K维的向量x表示,其中只有一维xk为1,其余为0。对应于xk=1的参数为μk,表示xk发生时的概率。其分布可以看做是伯努利分布的一般化。现在我们考虑N个独立的观测D={x1,…,xN},得到其似然函数。如图:

    2022年10月11日
    3
  • PHP结合Ueditor并修改图片上传路径

    PHP结合Ueditor并修改图片上传路径

    2021年9月18日
    41
  • Spring Cloud 学习笔记(2 / 3)

    Spring Cloud 学习笔记(2 / 3)SpringCloud学习笔记(1/3)SpringCloud学习笔记(3/3)—56_Hystrix之全局服务降级DefaultProperties57_Hystrix之通配服务降级FeignFallback58_Hystrix之服务熔断理论59_Hystrix之服务熔断案例(上)60_Hystrix之服务熔断案例(下)61_Hystrix之服务熔断总结62_Hystrix工作流程最后总结63_Hystrix图形化Dashboard搭建

    2022年6月2日
    32

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号