PDA

View Full Version : Receive varialbe length Image Problem??



ganapathi
10th February 2016, 10:55
The following code is intended to display an Image sent over network. I sent a header of 16 bytes which I use to calculate the size of image that follows and then read that many bytes and display the image.


void socket::readyRead()
{

while(socket->bytesAvailable() > 0) {
quint8 Data[16];

socket->read((char *)&Data,16);
img_size = (((quint8)Data[1]<<8)+ (quint8)Data[0]) * (((quint8)Data[3]<<8)+ (quint8)Data[2]) * 1;
QByteArray buffer = socket->read(img_size);

qDebug() << "image zi" << img_size+1;

while(buffer.size() < (img_size))
{
// qDebug() << buffer.size();
socket->waitForReadyRead();
buffer.append(socket->read((img_size)-(buffer.size()) ));
}
unsigned char* imgdatara = (unsigned char*)&buffer.data()[0];
if( !image )
image = new QImage(imgdatara,32,640,QImage::Format_Grayscale8) ;
else
{
delete image;
image = new QImage(imgdatara,32,640,QImage::Format_Grayscale8) ;
}


emit msg(image);


}

}

My GUI says "not responding" and freezes.

Lesiok
10th February 2016, 11:25
First of all when you go into socket::readyRead() method there is no guarantee that it received 16 bytes (full header). It may be 1 byte or 1000 bytes.

ganapathi
10th February 2016, 11:41
First of all when you go into socket::readyRead() method there is no guarantee that it received 16 bytes (full header). It may be 1 byte or 1000 bytes.
Ok Lesiok....

I made the following changes:




void socket::readyRead()
{

while(socket->bytesAvailable() > 0) {

QByteArray Data;
QByteArray buffer

while(socket->bytesAvailable < 16) //// I wait til 16 bytes header arrives and then do socket->read(16)
{
socket->waitForReadyRead();
}
Data = socket->read(16);

img_size = (((quint8)Data[1]<<8)+ (quint8)Data[0]) * (((quint8)Data[3]<<8)+ (quint8)Data[2]) * 1;


while(buffer.size() < (img_size))
{

socket->waitForReadyRead();
buffer.append(socket->read((img_size)-(buffer.size()) ));
}
unsigned char* imgdatara = (unsigned char*)&buffer.data()[0];
if( !image )
image = new QImage(imgdatara,32,640,QImage::Format_Grayscale8) ;
else
{
delete image;
image = new QImage(imgdatara,32,640,QImage::Format_Grayscale8) ;
}


emit msg(image);


}

}



This code still does not work.I am unable to find what is wrong even though I do understand how TCP receives message.

^NyAw^
10th February 2016, 11:53
Hi,

First, use a buffer to store the data from the socket.

Into "readyRead()" method, first check if you have almost 16 bytes into the socket.
If you have 16 bytes into the socket you can read all available bytes and store it into your buffer.
After this you can check if the buffer size have the bytes that your header points to plus 16 bytes(header). If not, you will receive another "readyRead" signal (it's like a loop).
When the buffer contains all the bytes you can create the image and don't forget to clear the buffer for the next receiving image.

ganapathi
11th February 2016, 09:01
Hi,

First, use a buffer to store the data from the socket.

Into "readyRead()" method, first check if you have almost 16 bytes into the socket.
If you have 16 bytes into the socket you can read all available bytes and store it into your buffer.
After this you can check if the buffer size have the bytes that your header points to plus 16 bytes(header). If not, you will receive another "readyRead" signal (it's like a loop).
When the buffer contains all the bytes you can create the image and don't forget to clear the buffer for the next receiving image.


Yes this worked ....thanks a lot..