Re: cannot read correct len from socket
If you guys take a look at the code above, you will realize, I am doing the same thing. I think this is bug irrelevant to my code. Besides, with your explanation, I do not think my code is wrong at all.
client:
Code:
int size = blob.length();
clientSocket->write((char*)&size, 4);
clientSocket->write(blob.data(), size);
server:
Code:
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;
Re: cannot read correct len from socket
Quote:
Originally Posted by
saman_artorious
I do not think my code is wrong at all.
And yet it is wrong.
Code:
int size = blob.length();
clientSocket->write((char*)&size, 4);
int size;
int x = clientConnection->read((char*)&size, 4);
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.
Code:
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;
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.
Re: cannot read correct len from socket
Quote:
Originally Posted by
saman_artorious
If you guys take a look at the code above, you will realize, I am doing the same thing.
I don't see any code of yours that waits for all the data to arrive.
Quote:
I think this is bug irrelevant to my code.
Please, don't continue. Just get a book and read it.
Quote:
Besides, with your explanation, I do not think my code is wrong at all.
Well, it doesn't work so surely it is not correct.
Quote:
Code:
clientSocket->write((char*)&size, 4);
Wrong! What about byte order?
Quote:
Code:
int x = clientConnection->read((char*)&size, 4);
Wrong! What if less than four bytes are available in the socket? What about byte order?
Quote:
Code:
char* buffer = new char[size];
Crashes if size is negative or larger than available memory.
Quote:
Code:
while(size2 > 0)
{
int r = clientConnection->read(buffer+offset, size2);
if(r == 0)
break;
size2 -= r;
offset += r;
}
qDebug() << size;
Wrong! What if less than "size" bytes are available in the socket?