我正在创建一个应用程序,我需要通过tcp发送一些图像。
发送部分是
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通信正常,因为只包含文本的其他消息可以通过ok ...
有什么想法吗?
提前致谢!
答案 0 :(得分: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.");
}
}
}