volatile_ah
28th July 2009, 11:55
Hey guys,
First up I'm new to this whole Qt-programming, but it's been a fun challenge thus far.
What I'm trying to do is send a QImage across the network using a QTcpSocket. Let's for a second pretend I don't care about bandwidth, I just want to try and get this working.
Below is my server code (snippet):
QBuffer image_buffer;
QImageWriter writer(&image_buffer, "PNG");
if(!writer.write(captured_image.getImage()))
qWarning()<<"ERROR: Unable to write image to buffer: "<<writer.errorString();
QByteArray data;
QDataStream out(&data, QIODevice::WriteOnly);
out.setVersion(QDataStream::Qt_4_0);
out<<(quint32)image_buffer.data().size();
qDebug()<<"Image size = "<<image_buffer.data().size();
out<<image_buffer.data();
video_socket.write(data);
if(!video_socket.waitForBytesWritten())
qWarning()<<"ERROR: Unable to send image: "<<video_socket.errorString();
My client code (snippet) is as follows:
QDataStream in(&video_socket);
in.setVersion(QDataStream::Qt_4_0);
while(video_socket.bytesAvailable() < sizeof(quint32))
{
qDebug()<<"Waiting for image size...";
if(!video_socket.waitForReadyRead())
{
qWarning()<<"ERROR: Failed to read image size: "<<video_socket.errorString();
return;
}
}
quint32 image_size;
in>>image_size;
qDebug()<<"Image Size = "<<image_size;
while(video_socket.bytesAvailable() < image_size)
{
qDebug()<<"Waiting for image...";
if(!video_socket.waitForReadyRead())
{
qWarning()<<"ERROR: Failed to read image: "<<video_socket.errorString();
return;
}
}
QByteArray data;
in>>data;
qDebug()<<"Data size = "<<data.size();
QBuffer image_buffer(&data);
QImageReader reader(&image_buffer, "PNG");
QImage received_image;
if(!reader.read(&received_image))
{
qWarning()<<"ERROR: Unable to read received image from buffer: "<<reader.errorString();
return;
}
However, the client only seems to be able to read the size of the image, then dies.
Below is the output from the qDebug() statements in the server:
Image size = 384387
Data written = 384395
And below is my output from qDebug() statements in the client program:
Waiting for image size...
Image Size = 384387
Waiting for image...
Waiting for image...
Waiting for image...
ERROR: Failed to read image: "The remote host closed the connection"
So I guess my question is, can anyone see why it is doing this? I'm at a loss...
Any help would be terrific!!!
Regards,
Adrian
First up I'm new to this whole Qt-programming, but it's been a fun challenge thus far.
What I'm trying to do is send a QImage across the network using a QTcpSocket. Let's for a second pretend I don't care about bandwidth, I just want to try and get this working.
Below is my server code (snippet):
QBuffer image_buffer;
QImageWriter writer(&image_buffer, "PNG");
if(!writer.write(captured_image.getImage()))
qWarning()<<"ERROR: Unable to write image to buffer: "<<writer.errorString();
QByteArray data;
QDataStream out(&data, QIODevice::WriteOnly);
out.setVersion(QDataStream::Qt_4_0);
out<<(quint32)image_buffer.data().size();
qDebug()<<"Image size = "<<image_buffer.data().size();
out<<image_buffer.data();
video_socket.write(data);
if(!video_socket.waitForBytesWritten())
qWarning()<<"ERROR: Unable to send image: "<<video_socket.errorString();
My client code (snippet) is as follows:
QDataStream in(&video_socket);
in.setVersion(QDataStream::Qt_4_0);
while(video_socket.bytesAvailable() < sizeof(quint32))
{
qDebug()<<"Waiting for image size...";
if(!video_socket.waitForReadyRead())
{
qWarning()<<"ERROR: Failed to read image size: "<<video_socket.errorString();
return;
}
}
quint32 image_size;
in>>image_size;
qDebug()<<"Image Size = "<<image_size;
while(video_socket.bytesAvailable() < image_size)
{
qDebug()<<"Waiting for image...";
if(!video_socket.waitForReadyRead())
{
qWarning()<<"ERROR: Failed to read image: "<<video_socket.errorString();
return;
}
}
QByteArray data;
in>>data;
qDebug()<<"Data size = "<<data.size();
QBuffer image_buffer(&data);
QImageReader reader(&image_buffer, "PNG");
QImage received_image;
if(!reader.read(&received_image))
{
qWarning()<<"ERROR: Unable to read received image from buffer: "<<reader.errorString();
return;
}
However, the client only seems to be able to read the size of the image, then dies.
Below is the output from the qDebug() statements in the server:
Image size = 384387
Data written = 384395
And below is my output from qDebug() statements in the client program:
Waiting for image size...
Image Size = 384387
Waiting for image...
Waiting for image...
Waiting for image...
ERROR: Failed to read image: "The remote host closed the connection"
So I guess my question is, can anyone see why it is doing this? I'm at a loss...
Any help would be terrific!!!
Regards,
Adrian