问题描述:

I am trying to develop a Qt program using sockets for client and server communication. I need help with figuring out why my server crashes when the client connects and the server tries to get a QString?

I have this code on the server side:

void Client::onReadyRead() {

qDebug() << "On ready read!";

QDataStream in(mSocket);

in.setVersion(QDataStream::Qt_4_0);

for (;;) {

if(!mBlockSize) {

if (mSocket->bytesAvailable() < sizeof(quint16)) break;

in >> mBlockSize;

}

if(mSocket->bytesAvailable() < mBlockSize) break;

qDebug() << "Package was recieved!";

qDebug() << "Block size: " << mBlockSize;

if(Table::INVALID_ID == mUser.id()) {

QString authStr;

in >> authStr;

qDebug() << "Recieved account data: " << authStr;

}

mBlockSize = 0;

}

}

And on the client side:

void Client::onConnected()

{

qDebug() << "Connected!";

QByteArray block;

QDataStream out(&block, QIODevice::WriteOnly);

//Reserve 2 bytes

out << (quint16)0 << mEmail << "|" << mPassword;

//Back to the beginning

out.device()->seek(0);

//Write a block size

out << (quint16)(block.size() - sizeof(quint16));

mSocket->write(block);

}

相关阅读:
Top