{
}
Client::~Client()
{
}
void Client::run()
{
connect(pSocket, SIGNAL(readyRead()), this, SLOT(onSocketReadyRead()));
pSocket->connectToHost("192.168.50.77", 1234);
if (!pSocket->waitForConnected())
{
qDebug()<<"Unable to connect";
return;
}
exec();
}
void Client::onSocketReadyRead()
{
short sSize;
char* data;
unsigned long lPacketIndex;
qint64 nBytes = pSocket->bytesAvailable();
qDebug()<<"onSocketReadyRead bytes="<<nBytes;
if (nBytes < 2)
return;
qDebug()<<"Bytes="<<nBytes;
pSocket->peek((char*)&sSize, 2);
qDebug()<<"Packet size="<<sSize;
if (sSize != 19998)
{
QMessageBox::critical(NULL, tr
("error"), tr
("This is it"));
pSocket->disconnectFromHost();
return;
}
if (nBytes < 2 + sSize)
{
qDebug()<<" not enough data in stream";
return;
}
data = new char[sSize];
pSocket->read((char*)&sSize, 2);
pSocket->read(data, sSize);
memcpy((char*)&lPacketIndex, data, 4);
qDebug()<<" data read, packet index="<<lPacketIndex;
delete data;
}
Client::Client(QObject *parent) : QThread(parent)
{
}
Client::~Client()
{
}
void Client::run()
{
pSocket = new QTcpSocket(this);
connect(pSocket, SIGNAL(readyRead()), this, SLOT(onSocketReadyRead()));
pSocket->connectToHost("192.168.50.77", 1234);
if (!pSocket->waitForConnected())
{
qDebug()<<"Unable to connect";
return;
}
exec();
}
void Client::onSocketReadyRead()
{
short sSize;
char* data;
unsigned long lPacketIndex;
qint64 nBytes = pSocket->bytesAvailable();
qDebug()<<"onSocketReadyRead bytes="<<nBytes;
if (nBytes < 2)
return;
qDebug()<<"Bytes="<<nBytes;
pSocket->peek((char*)&sSize, 2);
qDebug()<<"Packet size="<<sSize;
if (sSize != 19998)
{
QMessageBox::critical(NULL, tr("error"), tr("This is it"));
pSocket->disconnectFromHost();
return;
}
if (nBytes < 2 + sSize)
{
qDebug()<<" not enough data in stream";
return;
}
data = new char[sSize];
pSocket->read((char*)&sSize, 2);
pSocket->read(data, sSize);
memcpy((char*)&lPacketIndex, data, 4);
qDebug()<<" data read, packet index="<<lPacketIndex;
delete data;
}
To copy to clipboard, switch view to plain text mode
Bookmarks