发布于2021-07-24 22:03 阅读(605) 评论(0) 点赞(27) 收藏(1)
class TcpClient :public QTcpSocket
{
Q_OBJECT
public:
explicit TcpClient(QObject *parent = nullptr);
~TcpClient();
static TcpClient *Instance();
private:
static QScopedPointer<TcpClient> self;
QTcpSocket *tcpClient;
public:
bool connectTcpServer(const QString ip, int port);
bool disconnectTcpServer();
void sendData(const QString &data);
void sendData(const QByteArray &bytes);
void sendData(const QJsonObject &jsonobj);
void sentData(const QJsonArray &jsonarray);
void sentData(const QImage &image);
signals:
void readSverDate(const QByteArray &data);
void serverStart();
void serverStop();
private slots:
void readDate();
void connected();
void disconnected();
};
class TcpServer :public QTcpServer
{
Q_OBJECT
public:
explicit TcpServer(QObject *parent = nullptr);
~TcpServer();
static TcpServer *Instance();
private:
//*智能指针,在其生命期结束后会自动删除它所指的对象
static QScopedPointer<TcpServer> self;
QTcpServer *tcpServer;
QList<QTcpSocket *> tcpclients;
QTcpSocket *currentClient;
public:
bool startTcpServer(const QHostAddress &sIP, int iPort);
void stopTcpServer();
//指定断开
void stopTcpServer(const QString &ip, int port);
//指定连接发送数据 c++ 形参不同,形成函数重载
void sendData(const QString &ip, int port, const QString &data);
void sendData(const QString &data);
void sendData(const QString &ip, int port, const QByteArray &bytes);
void sendData(const QByteArray &bytes);
void sendData(const QString &ip, int port, const QJsonObject &jsonobj);
void sendData(const QJsonObject &jsonobj);
void sendData(const QString &ip, int port, const QJsonArray &jsonarray);
void sentData(const QJsonArray &jsonarray);
void sendData(const QString &ip, int port, const QImage &image);
void sentData(const QImage &image);
signals:
void receiveData(const QString &ip, int port, const QByteArray &data);
void clientConnected(const QString &ip, int port);
void clientDisconnected(const QString &ip, int port);
private slots:
void doclientConnected();
void doclientDisconnected();
void doreceiveData();
};
class UdpClient :public QUdpSocket
{
Q_OBJECT
public:
explicit UdpClient(QObject *parent = nullptr);
~UdpClient();
static UdpClient *Instance();
private:
QUdpSocket *udpclient;
static QScopedPointer<UdpClient> self;
public:
void sendData(const QString &ip, int port, const QString &data);
void sendData(const QString &ip, int port, const QByteArray &byte);
signals:
void doreadSverData(const QString &ip, int port, const QByteArray &data);
private slots:
void readSverData();
};
class UdpServer :public QUdpSocket
{
Q_OBJECT
public:
explicit UdpServer(QObject *parent = nullptr);
~UdpServer();
static UdpServer *Instance();
private:
QUdpSocket *udpserver;
static QScopedPointer<UdpServer> self;
public:
bool startUdpServer(const QString &ip, int port);
void stopUdpServer();
void sendData(const QString &ip, int port, const QString &data);
void sendData(const QString &ip, int port, const QByteArray &byte);
signals:
void doreadClientData(const QString &ip, int port, const QByteArray &data);
private slots:
void readClientData();
};
* 1: 四种模式,tcp客户端、tcp服务器、udp客户端、udp服务器
* 2:支持多个客户端连接并发
* 3:支持服务端单播,广播,指定断开,全部断开,断开后提示
* 4: TCP传输 json对象 byte数组 自定义结构体等!~
测试demo:
完整源码下载地址:
https://gitee.com/zlozl5566/qt-learning-notes/tree/master/
原文链接:https://blog.csdn.net/ZLOZL/article/details/109255131
作者:每个人身上都有毛毛
链接:http://www.pythonpdf.com/blog/article/398/6855707d5d530582c1f9/
来源:编程知识网
任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任
昵称:
评论内容:(最多支持255个字符)
投诉与举报,广告合作请联系vgs_info@163.com或QQ3083709327
免责声明:网站文章均由用户上传,仅供读者学习交流使用,禁止用做商业用途。若文章涉及色情,反动,侵权等违法信息,请向我们举报,一经核实我们会立即删除!