当前位置: 首页 > news >正文

张家口网站建设工作室软通动力外包值得去吗

张家口网站建设工作室,软通动力外包值得去吗,扎实做好机构编制网站建设,做韦恩图的网站为大家分享一下最近封装的以太网socket通讯接口 效果演示 如图,界面还没优化,后续更新 废话不多说直接上教程 添加库 如果为qmake项目中,在.pro文件添加 QT network QT core gui QT networkgreaterThan(QT_MAJOR_VERS…

为大家分享一下最近封装的以太网socket通讯接口

效果演示

如图,界面还没优化,后续更新

废话不多说直接上教程

添加库

如果为qmake项目中,在.pro文件添加   QT  += network

QT       += core gui
QT       += networkgreaterThan(QT_MAJOR_VERSION, 4): QT += widgetsCONFIG += c++17

Socket封装

头文件// SocketManager.h

#ifndef SOCKETMANAGER_H
#define SOCKETMANAGER_H#include <QObject>
#include <QTcpSocket>
#include <QTcpServer>
#include <QUdpSocket>
#include <functional>class SocketManager : public QObject
{Q_OBJECTpublic:enum class Protocol { TCP, UDP };explicit SocketManager(QObject *parent = nullptr);~SocketManager();// Set the callback for incoming messagesvoid setMessageCallback(const std::function<void(const QByteArray &)> &callback);// Initialize the socketbool initialize(Protocol protocol, const QString &host, quint16 port, bool isServer = false);// Send a messageqint64 sendMessage(const QByteArray &message);private slots:void onReadyRead();private:Protocol m_protocol;QTcpSocket *m_tcpSocket = nullptr;QTcpServer *m_tcpServer = nullptr;QUdpSocket *m_udpSocket = nullptr;std::function<void(const QByteArray &)> m_messageCallback;void handleIncomingConnection();
};#endif // SOCKETMANAGER_H

源文件// SocketManager.cpp


#include "SocketManager.h"SocketManager::SocketManager(QObject *parent): QObject(parent)
{
}SocketManager::~SocketManager()
{if (m_tcpSocket) delete m_tcpSocket;if (m_tcpServer) delete m_tcpServer;if (m_udpSocket) delete m_udpSocket;
}void SocketManager::setMessageCallback(const std::function<void(const QByteArray &)> &callback)
{m_messageCallback = callback;
}bool SocketManager::initialize(Protocol protocol, const QString &host, quint16 port, bool isServer)
{m_protocol = protocol;if (protocol == Protocol::TCP) {if (isServer) {m_tcpServer = new QTcpServer(this);connect(m_tcpServer, &QTcpServer::newConnection, this, &SocketManager::handleIncomingConnection);return m_tcpServer->listen(QHostAddress(host), port);} else {m_tcpSocket = new QTcpSocket(this);m_tcpSocket->connectToHost(host, port);connect(m_tcpSocket, &QTcpSocket::readyRead, this, &SocketManager::onReadyRead);return m_tcpSocket->waitForConnected();}} else if (protocol == Protocol::UDP) {m_udpSocket = new QUdpSocket(this);connect(m_udpSocket, &QUdpSocket::readyRead, this, &SocketManager::onReadyRead);return m_udpSocket->bind(QHostAddress(host), port);}return false;
}qint64 SocketManager::sendMessage(const QByteArray &message)
{if (m_protocol == Protocol::TCP && m_tcpSocket) {return m_tcpSocket->write(message);} else if (m_protocol == Protocol::UDP && m_udpSocket) {return m_udpSocket->writeDatagram(message, QHostAddress::Broadcast, m_udpSocket->localPort());}return -1;
}void SocketManager::onReadyRead()
{if (m_protocol == Protocol::TCP && m_tcpSocket) {QByteArray data = m_tcpSocket->readAll();if (m_messageCallback) {m_messageCallback(data);}} else if (m_protocol == Protocol::UDP && m_udpSocket) {while (m_udpSocket->hasPendingDatagrams()) {QByteArray data;data.resize(m_udpSocket->pendingDatagramSize());m_udpSocket->readDatagram(data.data(), data.size());if (m_messageCallback) {m_messageCallback(data);}}}
}void SocketManager::handleIncomingConnection()
{if (m_tcpServer) {m_tcpSocket = m_tcpServer->nextPendingConnection();connect(m_tcpSocket, &QTcpSocket::readyRead, this, &SocketManager::onReadyRead);}
}

使用方式

首先引入#include "SocketManager.h"

初始化

通过enum class Protocol { TCP, UDP };来选择TCP/UDP

通过第三个参数来决定如果是TCP的话是Service服务端还是Client客户端

    socketManager =  new SocketManager(this);if (socketManager->initialize(SocketManager::Protocol::UDP, "127.0.0.1", 12345, false)) {qDebug() << "TCP Client initialized.";}else{qDebug() << "TCP Client fail.";}socketManager->setMessageCallback([](const QByteArray &message) {qDebug() << "Received message:" << message;});

创建回调函数

收到消息通过回调函数的方式来接收数据

socketManager->setMessageCallback([](const QByteArray &message) {qDebug() << "Received message:" << message;});

 最后,我将项目放到Github,GitHub - xiugou798/QT6-Socket-Demo,欢迎大家优化修改

http://www.yayakq.cn/news/38053/

相关文章:

  • 企业网站建设合同电子版外包人员
  • 商超网站天津网站建设行业新闻
  • seo关键词优化怎么收费网站建设和网络优化请示
  • 最好的免费软件网站建设wordpress文章分页
  • 网站的组成Wordpress自动回复评论
  • 伊川网站建设怎么建设微网站
  • 网站开发团队简介如何写微信小程序免费制作平台
  • 网页免费建站四川省建设厅网站电话
  • 网站建站建设哪家好丹阳火车站对面规划
  • 温岭 网站建设做网站收会员费违法吗
  • 山东省建设执业师网站保定免费网站制作
  • 网站建设平台软件哪个好用网站ip pv
  • 免费注册网站怎么做链接软件技术毕业后能做什么工作
  • 韩雪个人网站阿里wordpress怎么安装
  • 黄石百度做网站多少钱怎么不用wordpress
  • 做网站做推广有效果吗外贸网站建设 东莞
  • 做网站申请域名空间大理微网站建设
  • 洛阳做公司网站wordpress移动端加底部导航
  • 做网站推广邢台电器 东莞网站建设
  • 网站开发可以多少钱一个月做网站开发有什么专业证
  • 古典风格中药医药企业网站模板源码网络营销工具和方法
  • 网站怎样做排名订阅号 微网站
  • 网站建设备案是什么意思wordpress制作app
  • 提供小企业网站建设html5网站开发开题报告
  • 搭建wordpress网站wordpress wp_query paged
  • 南宁网站平台怎么做竞拍网站
  • 做芯片哪个网站推广网络营销案例ppt
  • 丰县网站建设推广常用的网络推广方式有哪些
  • 广东高端网站建设报价优的网站建设明细报价表
  • 西北建设有限公司官方网站淘宝如何做推广