PDA

View Full Version : how to use qtcpsocket send qimage data



tsuibin
7th November 2010, 04:36
my tcpserver code


void UserThread::sendData()
{
qDebug() << "sendData";

#ifdef TEST
qDebug() << "TEST";
QImage image("3.png");
QByteArray ba;

qDebug() << image.byteCount() <<image.size();
ba.append((char *)image.bits(),image.byteCount());
qDebug() <<ba.size();
this->m_tcpSocket->write(ba);
if(!this->m_tcpSocket->waitForBytesWritten(-1))
{
qDebug() << "writen Bytes error " << this->m_tcpSocket->errorString();
}
this->m_tcpSocket->flush();
#endif
}


client code


void MainWindow::readImage(){
qDebug() << socket.bytesAvailable();
QByteArray ba;
ba = socket.readAll();
socket.flush();
qDebug() << ba.size();
QImage image((uchar *)ba.data(),1024,768,QImage::Format_RGB32);
ui->label->setPixmap(QPixmap::fromImage(image));
}


my picture at attachment

server read the picture sizse is 3145728

but client recevie a wrong size
i used flush
but ...

high_flyer
8th November 2010, 10:07
server read the picture sizse is 3145728
but client recevie a wrong size
Try looping on the clinet side until your buffer gets filled with the correct size.


//This is pseudo code
while((size < imageSize) && !bTimeout)
{
ba.append(socket.readAll());
}


you don't need to call flush() on the client side.

rtpavlovsky
18th April 2012, 14:51
server read the picture sizse is 3145728

but client recevie a wrong size

TCP will break up your bytearray into lots of different packets which you must append to the socket bytearray... you need them all for the complete image. (readAll() only reads all of the first packet).