82 lines
1.9 KiB
C++
82 lines
1.9 KiB
C++
|
#include "tcpclient.h"
|
|||
|
#include "quiwidget.h"
|
|||
|
|
|||
|
TcpClient::TcpClient(QTcpSocket *socket, QObject *parent) : QObject(parent)
|
|||
|
{
|
|||
|
this->socket = socket;
|
|||
|
ip = socket->peerAddress().toString();
|
|||
|
ip = ip.replace("::ffff:", "");
|
|||
|
port = socket->peerPort();
|
|||
|
|
|||
|
connect(socket, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(disconnected()));
|
|||
|
connect(socket, SIGNAL(disconnected()), this, SLOT(disconnected()));
|
|||
|
connect(socket, SIGNAL(readyRead()), this, SLOT(readData()));
|
|||
|
}
|
|||
|
|
|||
|
QString TcpClient::getIP() const
|
|||
|
{
|
|||
|
return this->ip;
|
|||
|
}
|
|||
|
|
|||
|
int TcpClient::getPort() const
|
|||
|
{
|
|||
|
return this->port;
|
|||
|
}
|
|||
|
|
|||
|
void TcpClient::disconnected()
|
|||
|
{
|
|||
|
socket->deleteLater();
|
|||
|
this->deleteLater();
|
|||
|
emit clientDisconnected();
|
|||
|
}
|
|||
|
|
|||
|
void TcpClient::readData()
|
|||
|
{
|
|||
|
QByteArray data = socket->readAll();
|
|||
|
if (data.length() <= 0) {
|
|||
|
return;
|
|||
|
}
|
|||
|
|
|||
|
QString buffer;
|
|||
|
if (App::HexReceiveTcpServer) {
|
|||
|
buffer = QUIHelper::byteArrayToHexStr(data);
|
|||
|
} else if (App::AsciiTcpServer) {
|
|||
|
buffer = QUIHelper::byteArrayToAsciiStr(data);
|
|||
|
} else {
|
|||
|
buffer = QString(data);
|
|||
|
}
|
|||
|
|
|||
|
emit receiveData(ip, port, buffer);
|
|||
|
|
|||
|
//自动回复数据,可以回复的数据是以;隔开,每行可以带多个;所以这里不需要继续判断
|
|||
|
if (App::DebugTcpServer) {
|
|||
|
int count = App::Keys.count();
|
|||
|
for (int i = 0; i < count; i++) {
|
|||
|
if (App::Keys.at(i) == buffer) {
|
|||
|
sendData(App::Values.at(i));
|
|||
|
break;
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
void TcpClient::sendData(const QString &data)
|
|||
|
{
|
|||
|
QByteArray buffer;
|
|||
|
if (App::HexSendTcpServer) {
|
|||
|
buffer = QUIHelper::hexStrToByteArray(data);
|
|||
|
} else if (App::AsciiTcpServer) {
|
|||
|
buffer = QUIHelper::asciiStrToByteArray(data);
|
|||
|
} else {
|
|||
|
buffer = data.toUtf8();
|
|||
|
}
|
|||
|
|
|||
|
socket->write(buffer);
|
|||
|
emit sendData(ip, port, data);
|
|||
|
}
|
|||
|
|
|||
|
void TcpClient::abort()
|
|||
|
{
|
|||
|
socket->abort();
|
|||
|
}
|