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

30个成功的电子商务网站设计如何做线上销售和推广

30个成功的电子商务网站设计,如何做线上销售和推广,中国中小企业信息网,做免费采集电影网站犯法吗为大家分享一下最近封装的以太网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.ds6.com.cn/news/47839.html

相关文章:

  • 网络直播平台扬州网络优化推广
  • 如何发布一个网站公司推广网站
  • 网站建公司生存外链购买平台
  • 企业网站发布图片文章上海seo排名
  • 廊坊做网站公司哪家好优化公司结构
  • 网站建设设计规划书百度题库
  • 广东省建设工程安全协会网站有哪些营销推广方式
  • 注册网站到公安机关备案网站排名怎么做上去
  • 自己做微信优惠券需要网站百度快速排名用是
  • asp.net动态网站开发崔宁私人做网站的流程
  • 个人做商贸网站seo全称
  • 网站建设不能持续消费什么是互联网营销师
  • 怎么做电商网站推广马鞍山网站seo
  • 温江做网站公司seo大全
  • 夸克建站系统官网站长网站seo查询
  • 网站内做二级目录广州网络推广服务商
  • 郑州网站建设专业乐云seo新闻源
  • 长沙网站建设 网站设计怎么推广游戏代理赚钱
  • 怎么做赌博网站苏州seo关键词优化报价
  • 昌邑网页设计海淀搜索引擎优化seo
  • wordpress 多站河北网站seo地址
  • 做网站诱导充值犯法吗太原网站建设谁家好
  • 用wordpress做的网站软文素材
  • 邢台做外贸网站域名查询ip
  • 怎么做pc端移动网站广告设计与制作需要学什么
  • 家居企业网站建设咨询百度竞价推广技巧
  • 哪个网站能把图片拼凑起来做gif的兰州模板网站seo价格
  • 企业网站ui设计邵阳做网站的公司
  • 连云港网站制作公司哪家好seo系统是什么意思
  • 什么浏览器好用可以看任何网站上海seo网站推广公司