well, while-loops are not network-specific, are they?
code taken from fortune client example
const int Timeout = 5 * 1000;
// wait for blocksize to arrive
while (socket.bytesAvailable() < (int)sizeof(quint16)) {
if (!socket.waitForReadyRead(Timeout)) {
emit error(socket.error(), socket.errorString());
return;
}
}
// read blockSize
quint16 blockSize;
in >> blockSize;
// blocksize does not include "itself"
// now we know how much data has to arrive in total
// wait for rest of the block...
while (socket.bytesAvailable() < blockSize) {
if (!socket.waitForReadyRead(Timeout)) {
emit error(socket.error(), socket.errorString());
return;
}
// now we are sure that no 'short reads' can happen
instream >> database;
const int Timeout = 5 * 1000;
// wait for blocksize to arrive
while (socket.bytesAvailable() < (int)sizeof(quint16)) {
if (!socket.waitForReadyRead(Timeout)) {
emit error(socket.error(), socket.errorString());
return;
}
}
// read blockSize
quint16 blockSize;
QDataStream in(&socket);
in.setVersion(QDataStream::Qt_4_0);
in >> blockSize;
// blocksize does not include "itself"
// now we know how much data has to arrive in total
// wait for rest of the block...
while (socket.bytesAvailable() < blockSize) {
if (!socket.waitForReadyRead(Timeout)) {
emit error(socket.error(), socket.errorString());
return;
}
// now we are sure that no 'short reads' can happen
instream >> database;
To copy to clipboard, switch view to plain text mode
you have to take care if blockSize is the size of the complete package (including the 2 bytes of the quint16 of blocksize itself) or just the data part's size. in the fortune server example blocksize is only the data part
HTH
Bookmarks