scoutycat
9th June 2016, 20:44
I am trying to read values from a socket. when stepping through, everything works as expected until the line "in>>data;". After this line, data is a null pointer. The basic logic is set the socket, wait for enough bytes to read the size of the packet, get the packet size, then read the rest of the data once it's been sent. Socket is a QTcpSocket * and mServer is a QTcpServer *. Checkbytes is just for debug, and it returns the expected value. I have no idea why the datastream isn't populating data??
socket = mServer->nextPendingConnection();
while (socket->bytesAvailable() < (int)sizeof(quint16))
{
if (!socket->waitForReadyRead(Timeout))
{
emit error(socket->error(), socket->errorString());
return;
}
};
quint16 blockSize;
QDataStream in(socket);
in.setVersion(QDataStream::Qt_5_6);
in.setByteOrder(QDataStream::LittleEndian);
char buf[(int)sizeof(quint16)];
socket->peek(buf, sizeof(buf));
blockSize = (*(buf +1) <<8) | *(buf);
while (socket->bytesAvailable() < blockSize )
{
if (!socket->waitForReadyRead(Timeout))
{
emit error(socket->error(), socket->errorString());
return;
}
};
int checkbytes = socket->bytesAvailable(); //this is the expected number
char* data;
in >> data;
//datastream >> allocates using new - caller must destroy
delete[] data;
socket = mServer->nextPendingConnection();
while (socket->bytesAvailable() < (int)sizeof(quint16))
{
if (!socket->waitForReadyRead(Timeout))
{
emit error(socket->error(), socket->errorString());
return;
}
};
quint16 blockSize;
QDataStream in(socket);
in.setVersion(QDataStream::Qt_5_6);
in.setByteOrder(QDataStream::LittleEndian);
char buf[(int)sizeof(quint16)];
socket->peek(buf, sizeof(buf));
blockSize = (*(buf +1) <<8) | *(buf);
while (socket->bytesAvailable() < blockSize )
{
if (!socket->waitForReadyRead(Timeout))
{
emit error(socket->error(), socket->errorString());
return;
}
};
int checkbytes = socket->bytesAvailable(); //this is the expected number
char* data;
in >> data;
//datastream >> allocates using new - caller must destroy
delete[] data;