dfink
1st December 2009, 11:48
Hi,
I'm trying to send images over a network connection the easiest way possible. Its not a Video stream and its only local network, so the bandwidth usage is not that important.
I've tried two approaches till now:
First using the overloaded << operator "QDataStream << QImage"
Client Side:
// QImage image is already instantiated
_socket->connectToHost("127.0.0.1", 11111, QIODevice::ReadWrite);
QDataStream stream(_socket);
stream << image;
_socket->waitForBytesWritten(-1);
Server Side:
QAbstractSocket *socket = _server.nextPendingConnection();
QDataStream stream(socket, QIODevice::ReadWrite);
socket->waitForReadyRead(-1);
stream >> inQImage;
if (inQImage.isNull()){
qDebug("Received Image is empty");
}
Result:
libpng error: Read Error
Received Image is empty
Second Approach using the QImageWriter and QImageReader:
Client Side:
QImageWriter writer(socket, "png");
writer.write(image);
socket->waitForBytesWritten(-1);
Server Side:
_socket->waitForReadyRead(-1);
QImageReader imReader(_socket,"png");
imReader.read(_image);
_image.save("received.png","png");
The Result is also an libpng error: Read Error but the Image is partially transferred. When using a sufficiently small image it works.
It also works when the IODevice is not a QTcpSocket but a QFile
In both cases is seams that not all data is sent, is there a possibility that waitForBytesWritten() is not working as expected?
thanks in advance
Daniel
I'm trying to send images over a network connection the easiest way possible. Its not a Video stream and its only local network, so the bandwidth usage is not that important.
I've tried two approaches till now:
First using the overloaded << operator "QDataStream << QImage"
Client Side:
// QImage image is already instantiated
_socket->connectToHost("127.0.0.1", 11111, QIODevice::ReadWrite);
QDataStream stream(_socket);
stream << image;
_socket->waitForBytesWritten(-1);
Server Side:
QAbstractSocket *socket = _server.nextPendingConnection();
QDataStream stream(socket, QIODevice::ReadWrite);
socket->waitForReadyRead(-1);
stream >> inQImage;
if (inQImage.isNull()){
qDebug("Received Image is empty");
}
Result:
libpng error: Read Error
Received Image is empty
Second Approach using the QImageWriter and QImageReader:
Client Side:
QImageWriter writer(socket, "png");
writer.write(image);
socket->waitForBytesWritten(-1);
Server Side:
_socket->waitForReadyRead(-1);
QImageReader imReader(_socket,"png");
imReader.read(_image);
_image.save("received.png","png");
The Result is also an libpng error: Read Error but the Image is partially transferred. When using a sufficiently small image it works.
It also works when the IODevice is not a QTcpSocket but a QFile
In both cases is seams that not all data is sent, is there a possibility that waitForBytesWritten() is not working as expected?
thanks in advance
Daniel