2012-12-12 51 views
0

我正在创建一个应用程序,我需要通过tcp发送一些图像。通过qtcpsocket发送qimage:只接收部分数据

发送部分是

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(); 

和客户端:

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

的问题是,当服务器似乎被发送的所有数据, 客户端接收仅将报头..
(当然程序崩溃在线

image.loadFromData(buffer.data());

的TCP通信工程确定,因为只包含文字经过确定的其他消息...

任何想法?

在此先感谢!

回答

0

...并回答我的问题,如果任何人有同样的问题...

我需要设置接收端从套接字读取,直到我得到了所有的数据...

这样的东西:

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