简体   繁体   English

通过qtcpsocket发送qimage:仅接收部分数据

[英]sending qimage over qtcpsocket: received only part of the data

i am creating an application where i need to send some images over tcp. 我正在创建一个应用程序,我需要通过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()); image.loadFromData(buffer.data());

The tcp communication works ok, since other messages containing only text go through ok... tcp通讯正常,因为其他仅包含文本的消息也可以通过...

Any ideas? 有任何想法吗?

Thanks in advance! 提前致谢!

...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.");
      }
   }
}   

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM