当前位置:网站首页>Wheel 7: TCP client
Wheel 7: TCP client
2022-07-28 22:51:00 【Lao Zhao's blog】
#ifndef TCPCMDCLIENT_H
#define TCPCMDCLIENT_H
#include <QObject>
#include <QTcpSocket>
#include "CustomFrameBuffer/CustomFrameBufferFor48s.h"
class TcpCmdClient : public QObject
{
Q_OBJECT
public:
explicit TcpCmdClient(QObject *parent = nullptr);
~TcpCmdClient();
void setServerIpPort(QString strIp, quint16 port);
signals:
// Will receive Tcp Data transmission to business interface class
void sig_TransferData(QByteArray ba);
public slots:
void slot_InitSocket();
void slot_onConnected();
void slot_onDisconnected();
void slot_recvData();
// Network error
void slot_networkError(QAbstractSocket::SocketError err);
void slot_sendData(QByteArray ba);
private:
void connectTcpServer();
private:
QTcpSocket *m_pClientSocket;//tcp Client object pointer
QString m_strServerIP;//tcp Server side ip
quint16 m_uServerPort;//tcp Server port
bool m_bConnect;// Whether to connect identification
CustomFrameBufferFor48s m_FrameBuffer;
};
#endif // TCPCMDCLIENT_H
#include "TcpCmdClient.h"
#include "Player.h"
TcpCmdClient::TcpCmdClient(QObject *parent) : QObject(parent)
{
m_bConnect = false;
m_strServerIP = "";
m_uServerPort = 0;
m_pClientSocket = nullptr;
}
TcpCmdClient::~TcpCmdClient()
{
if(m_pClientSocket)
{
m_pClientSocket->abort();
m_pClientSocket->deleteLater();
}
}
void TcpCmdClient::setServerIpPort(QString strIp, quint16 port)
{
m_strServerIP = strIp;
m_uServerPort = port;
}
void TcpCmdClient::connectTcpServer()
{
if(nullptr==m_pClientSocket)
{
m_pClientSocket = new QTcpSocket;
connect(m_pClientSocket,&QTcpSocket::readyRead,this,&TcpCmdClient::slot_recvData);
connect(m_pClientSocket,&QTcpSocket::connected,this,&TcpCmdClient::slot_onConnected);
connect(m_pClientSocket,&QTcpSocket::disconnected,this,&TcpCmdClient::slot_onDisconnected);
connect(m_pClientSocket,SIGNAL(error(QAbstractSocket::SocketError)),this,SLOT(slot_networkError(QAbstractSocket::SocketError)));
}
m_pClientSocket->abort();
//m_pClientSocket->setProxy(QNetworkProxy::NoProxy);
m_pClientSocket->connectToHost(m_strServerIP,m_uServerPort);
m_bConnect = m_pClientSocket->waitForConnected(5000);
if(m_bConnect)
{
AddLog(QStringLiteral(" Connect tcp Server success "),QString("ip=%1 port=%2").arg(m_strServerIP).arg(m_uServerPort));
}
else
{
AddLog(QStringLiteral(" Connect tcp The server failed "),QString("ip=%1 port=%2").arg(m_strServerIP).arg(m_uServerPort));
}
}
void TcpCmdClient::slot_InitSocket()
{
connectTcpServer();
}
void TcpCmdClient::slot_onConnected()
{
m_bConnect = true;
}
void TcpCmdClient::slot_onDisconnected()
{
m_bConnect = false;
AddLog(QStringLiteral(" Connect tcp The server is disconnected "),"");
}
void TcpCmdClient::slot_networkError(QAbstractSocket::SocketError err)
{
AddLog(QStringLiteral(" Connect tcp An error occurred on the server "),QString("err = %1").arg(err));
}
void TcpCmdClient::slot_recvData()
{
while((m_pClientSocket->bytesAvailable())>0)
{
QByteArray readmsg = m_pClientSocket->readAll();
//qDebug()<<readmsg.size();
m_FrameBuffer.Write(readmsg.data(),readmsg.size());
QByteArray baFrame;
while(1){
int ret = m_FrameBuffer.GetFrame(baFrame);
if(ret == 1){
// Send a complete frame to businessIf Parsing
emit sig_TransferData(baFrame);
//qDebug() << "m_FrameBuffer.GetFrame is complete.";
}else{
//qDebug() << "m_FrameBuffer.GetFrame is not complete.";
break;
}
}
}
}
void TcpCmdClient::slot_sendData(QByteArray ba)
{
if(m_bConnect)
{
m_pClientSocket->write(ba.data(),ba.size());
}
}
边栏推荐
- Yolov5 improvement 4: add ECA channel attention mechanism
- STM32CUBEIDE(10)----ADC在DMA模式下扫描多个通道
- 775. Inverted words
- Differernet [anomaly detection: normalizing flow]
- 从 IPv4 向 IPv6 的迁移
- [get mobile information] - get mobile information through ADB command
- ES6, deep copy, shallow copy
- C language to realize string reverse order arrangement
- JVM——自定义类加载器
- Multi activity disaster recovery construction after 713 failure of station B | takintalks share
猜你喜欢

STM32 - Basic timer (tim6, tim7) working process, interpretation function block diagram, timing analysis, cycle calculation
![Ocr-gan [anomaly detection: Reconstruction Based]](/img/16/62d962288c192b3df2fdb518d7127e.gif)
Ocr-gan [anomaly detection: Reconstruction Based]

Imx6q GPIO multiplexing

Gd32f303 firmware library development (10) -- dual ADC polling mode scanning multiple channels

Migration from IPv4 to IPv6

Lvs+keepalived high availability deployment practical application
![[virtual machine _2]-hyper-v and vmware/virtualbox cannot coexist](/img/90/c481a817dc91d7e5247dd8e3ee1dae.png)
[virtual machine _2]-hyper-v and vmware/virtualbox cannot coexist

【转载】token令牌在登录场景使用

B站713故障后的多活容灾建设|TakinTalks大咖分享

Command line agent: proxychains configuration
随机推荐
How to use sprintf function
Awk blank line filtering
STM32 - external interrupt application (exti) (use cubemx to configure interrupts)
STM32 - DMA direct memory access controller (cubemx configures DMA)
Annaconda installs pytoch and switches environments
Paddlenlp is based on ernir3.0 text classification. Take the crime prediction task of cail2018-small dataset as an example [multiple tags]
Improvement 16 of yolov5: replace backbone network C3 with lightweight network pp-lcnet
776. String shift inclusion problem
STM32 - systick timer (cubemx configures systick)
OSV-q ValueError: axes don‘t match array
Leetcode exercise 3 - palindromes
一份来自奎哥的全新MPLS笔记,考IE必看 ----尚文网络奎哥
JVM -- custom class loader
Multi activity disaster recovery construction after 713 failure of station B | takintalks share
JSON file to PNG image (batch conversion / image naming / migration / pixel value change) [tips]
Memseg [anomaly detection: embedded based]
Introduction to structure
How do we do full link grayscale on the database?
Using nodejs to operate MySQL
Ngx+sql environment offline installation log (RPM installation)