#include "SocketClientUtil.h" SocketClientUtil::SocketClientUtil(QObject *parent) : QObject(parent) { QObject::connect(&objClient, &QTcpSocket::readyRead, this, &SocketClientUtil::readData); } void SocketClientUtil::connect(QString host, int port) { this->host = host; this->port = port; objClient.connectToHost(this->host, this->port); } QByteArray SocketClientUtil::getResponse() { return this->response; } void SocketClientUtil::sendData(QByteArray data) { data.append(0x0D).append(0x0A); // 自动加\r\n标识一帧结束 objClient.write(data); } void SocketClientUtil::readData() { QByteArray buffer = objClient.readAll(); this->response = buffer; emit this->responseReaded(); }