[Qt-interest] Why debugging read works?
Gabriele Kahlout
gabriele at mysimpatico.com
Sat May 22 00:02:34 CEST 2010
Hello,
I've written code that works in the debugger, but not otherwise!
Basically, I'm trying to deliver a light image (screenshot) from a client to
a server, via tcp. The algorithm goes as follows:
1. The server is listening for incoming connections.
2. The client connects.
3. The server accepts the incoming connection.
4. The client send the screenshot.
5. The server reads it in, receiving the readyread.
The 5th step works fine in the debugger, i.e. when there is a breakpoint
before the read, which when I manually step over, the image is correctly
read and displayed.
I've used code from the tutorials for the read and writes, reported below.
Could some one point out why this might be happening, and if so how could I
solve the problem?
I'm wondering if I need to synchronize the multiple writes and reads that
occur in:
while (socket->bytesAvailable() < blockSize) {
if (!socket->waitForReadyRead(Timeout)) {
If so, I wouldn't know how. A waitforbyteswritten after the write, helps
only at blocking the client. Maybe I should break the write into smaller
ones, and waitForBytesWritten? But written occurs at the socket, it's not
waitForBytesRead. Your guidance is truly appreciated.
qint64 write(QByteArray * array, QTcpSocket * socket){
QByteArray block;
QDataStream out(&block, QIODevice::WriteOnly);
out.setVersion(QDataStream::Qt_4_0);
out << (quint16)0;
out << *array;
out.device()->seek(0);
out << (quint16)(block.size() - sizeof(quint16));
qint64 ret = socket->write(block);
if(ret == -1)
throw runtime_error(socket->errorString().toStdString().c_str());
return ret;
}
QByteArray * read(QTcpSocket * socket){
const int Timeout = -1;//5 * 1000, no difference.
while (socket->bytesAvailable() < (int)sizeof(quint16)) {
if (!socket->waitForReadyRead(Timeout)) {
error(socket->error(), socket->errorString());
return 0;
}
}
quint16 blockSize;
QDataStream in(socket);
in.setVersion(QDataStream::Qt_4_0);
in >> blockSize;
while (socket->bytesAvailable() < blockSize) {
if (!socket->waitForReadyRead(Timeout)) {
error(socket->error(), socket->errorString());
return 0;
}
}
QByteArray * receivedSS = new QByteArray();
in >> *receivedSS;
return receivedSS;
}
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20100522/9e07dd90/attachment.html
More information about the Qt-interest-old
mailing list