void Client::onSocketReadyRead()
{
short sSize;
char* data;
unsigned long lPacketIndex;
qint64 nBytes = pSocket->bytesAvailable();
qDebug()<<"onSocketReadyRead bytes="<<nBytes;
if (nBytes < 2) //we haven't read enough data to get the buffer size, return
return;
qDebug()<<"Bytes="<<nBytes;
pSocket->peek((char*)&sSize, 2); //we read 2 bytes, we get the size of the buffer INSIDE these 2 bytes
//here i use peek to get the 2 bytes containing the size, without removing it from the buffer, because i may have not enough data, and i'll have to redo it.
qDebug()<<"Packet size="<<sSize;
/* this IF is only to get easily the error . It's DEBUG code.
as i KNOW the size must be 19998, if it's not, there was an error.
*/
if (sSize != 19998)
{
//i put a breakpoint here, the code stops.
QMessageBox::critical(NULL, tr
("error"), tr
("This is it"));
pSocket->disconnectFromHost();
return;
}
if (nBytes < 2 + sSize) //here we wait to have enough data to read sSize bytes
{
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;
}
void Client::onSocketReadyRead()
{
short sSize;
char* data;
unsigned long lPacketIndex;
qint64 nBytes = pSocket->bytesAvailable();
qDebug()<<"onSocketReadyRead bytes="<<nBytes;
if (nBytes < 2) //we haven't read enough data to get the buffer size, return
return;
qDebug()<<"Bytes="<<nBytes;
pSocket->peek((char*)&sSize, 2); //we read 2 bytes, we get the size of the buffer INSIDE these 2 bytes
//here i use peek to get the 2 bytes containing the size, without removing it from the buffer, because i may have not enough data, and i'll have to redo it.
qDebug()<<"Packet size="<<sSize;
/* this IF is only to get easily the error . It's DEBUG code.
as i KNOW the size must be 19998, if it's not, there was an error.
*/
if (sSize != 19998)
{
//i put a breakpoint here, the code stops.
QMessageBox::critical(NULL, tr("error"), tr("This is it"));
pSocket->disconnectFromHost();
return;
}
if (nBytes < 2 + sSize) //here we wait to have enough data to read sSize bytes
{
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