
Originally Posted by
saman_artorious
I do not think my code is wrong at all.
And yet it is wrong.
int size = blob.length();
clientSocket->write((char*)&size, 4);
int size;
int x = clientConnection->read((char*)&size, 4);
int size = blob.length();
clientSocket->write((char*)&size, 4);
int size;
int x = clientConnection->read((char*)&size, 4);
To copy to clipboard, switch view to plain text mode
This is not portable. The size of an int is not necessarily 4 bytes. That is why I mentionned quint32 in my previous post. In addition, this fails if the byte orders are not the same on the server and client machines. That is why I mentioned endianness in my previous post.
int size;
int y = clientConnection->bytesAvailable();
qDebug() << y;
int x = clientConnection->read((char*)&size, 4);
int size2 = size;
int offset = 0;
char* buffer = new char[size];
while(size2 > 0)
{
int r = clientConnection->read(buffer+offset, size2);
if(r == 0)
break;
size2 -= r;
offset += r;
}
qDebug() << size;
int size;
int y = clientConnection->bytesAvailable();
qDebug() << y;
int x = clientConnection->read((char*)&size, 4);
int size2 = size;
int offset = 0;
char* buffer = new char[size];
while(size2 > 0)
{
int r = clientConnection->read(buffer+offset, size2);
if(r == 0)
break;
size2 -= r;
offset += r;
}
qDebug() << size;
To copy to clipboard, switch view to plain text mode
QTcpSocket is non blocking by default. When read() returns 0, you must return to the event loop and wait for the readyRead() signal before you can read more data. That is why I mentioned asynchronous-style communication, discussed the storage of the state of your receiver, and provided a link to some explanations on the stream-oriented nature of TCP in my previous post.
Now, are you certain that you have read the material I linked to, and that you have experience in socket programming?
Also I recommend reading the documentation of the Qt classes you use (QIODevice, QAbstractSocket, QTcpSocket, ...) and the various networking examples.
Bookmarks