0

i am creating an application where i need to send some images over tcp.

the sending part is

QImage image;  
image.load("image.png", "PNG");  
image.setText("name", "color");  
QByteArray ba;  
QBuffer buffer(&ba);  
image.save(&buffer, "PNG");  
int bsize = ba.size();  
sendData(MESSAGE_BACKGROUND_COLOR, QString::number(ba.size()).toStdString());  
int bsent = m_tcpSocket->write(ba, ba.size());  
if(!m_tcpSocket->waitForBytesWritten(-1))  
{  
    qDebug() << "written Bytes error " << m_tcpSocket->errorString();  
}  
m_tcpSocket->flush();  

and the client:

 QByteArray buffer;  
 buffer = m_ConnectedClients[i]->read(messageSize);  
 int bytesS = buffer.size();  
 QImage image;  
 image.loadFromData(buffer.data());  
 if (image.isNull())  
 {  
    qDebug("The image is null. Something failed.");  
 }  

The problem is that where the server seems to be sending all the data, the client receives only the header...
(and of-course the program crashes in line

image.loadFromData(buffer.data());

The tcp communication works ok, since other messages containing only text go through ok...

Any ideas?

Thanks in advance!

stavrop
  • 465
  • 2
  • 8
  • 20

1 Answers1

0

...and answering my own question, in case anyone else has the same problem...

i needed to set the receiving end to read from the socket until i got all the data...

something like:

int bytesS = 0;
while(byteS < messageSize)
{
   buffer->append(m_ConnectedClients[i]->readAll());
   bytesS = buffer->size();
   if (bytesS == messageSize)
   {
      QImage image;
      image.loadFromData(*m_ConnectedClients[i]->buffer, "PNG");
      if (image.isNull())
      {
         qDebug("The image is null. Something failed.");
      }
   }
}   
stavrop
  • 465
  • 2
  • 8
  • 20