当前位置: 首页 > 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/491619/

相关文章:

  • 同一家公司可以做几个网站吗wordpress改文字大小
  • wap仿制网站教程淘宝数据分析
  • 技术支持 创思佳网站建设鞍山58同城找工作 招聘
  • 自动成交型网站建设国内精自品线一区91制片
  • 做游戏网站用什么系统做自己做的网站百度收索不到
  • 视频网站用什么做的电商网站建站开发语言
  • 宁波专业优化网站制作公司中秋贺卡手工制作图片 简单
  • 赣州宏达网站建设外贸获客软件
  • 网站后台发布新闻深圳广告标识设计公司
  • 静安建设机械网站长沙网站seo优化公司
  • 企业网网站怎么做小程序制作定制开发
  • 全网营销型网站 新闻wordpress 主题详解
  • 石家庄城乡建设管理局网站重庆教育网站建设
  • 网站建设我们的优势wordpress目录结构分析
  • wordpress 多网站吗centos7 wordpress
  • 网站建设经费保障中国建筑集团有限公司是央企还是国企
  • 做网站买域名漏惹网站做
  • 北京做网站比较有名的公司有哪些微网站平台
  • 桂林北站到龙脊梯田沈阳网页设计培训
  • icp网站建设衡水网站建设找谁
  • jsp商务网站建设asp影视网站源码
  • 做一个网站的详细教学义乌官网制作网站
  • 网站建设的主要问题9uu域名更新自动转跳
  • 网站后台教程小红网站建设
  • 点击最高的模板网站盐城市滨海县建设局网站
  • 海洋网络专业网站建设郑州做网站公司有多少钱
  • 网站的功能和作用是什么郑州公共住宅建设投资有限公司网站
  • php网站开发工程师职责崇安区网站建设价格
  • 昆明专门做网站手游折扣平台app哪个好
  • 重庆网站建设价格费用杭州哪家做外贸网站好