Correct way to read TCP packet :socket->readAll()?



  • Hi

    Client is sending an image(768 by 1024) by breaking them into stripes of 768 by 20 ie image block size is 20 using a TCP socket:

    Client_Image::Client_Image()
    {

    socket = new QTcpSocket(this);
    a = 0;
    img_block = 0;
    block_size = 20;
    
    socket->connectToHost("127.0.0.1",1234);
    
     image = new QImage("C:/Users/Public/Pictures/Sample Pictures/Desert.jpg" );
    
         if( image->isNull() )
        qDebug() << )"Failed to open test.jpg" ;
        else
         qDebug() << "Opened the test.jpg" ;
    
         QTimer *timer = new QTimer(this );
         timer->isSingleShot();
         QTimer::singleShot(1000, this, SLOT(broadcastLine()) );
        connect( this, SIGNAL(done()), this, SLOT(broadcastLine()) );
    

    }

    void Client_Image::broadcastLine()
    {

       QByteArray buffer( block_size+3*image->width(), 0 );
      QDataStream stream( &buffer, QIODevice::WriteOnly );
      stream.setVersion( QDataStream::Qt_5_5 ); 
    
     stream << (quint16)image->width() << (quint16)image->height();
     stream << img_block;
    
       for( int x=0; x<image->width(); ++x )
      {
          for(int y=block_size*a; y <block_size+(block_size*a);y++)
          {
              if( y < image->height())
              {
               QRgb rgb = image->pixel( x, y );
      stream << (quint8)qRed( rgb ) << (quint8)qGreen( rgb ) << (quint8)qBlue( rgb );
              }
              else
              {
                  y = a = 0;
                  img_block = -block_size;
              }
          }
      }
    
    
    socket->write(buffer);
    qDebug() << buffer.size() << "buffer size";
    
     socket->waitForBytesWritten(10);
     socket->flush();
     img_block = img_block + block_size;  
     a++;
    
     emit done();
    

    }

    I receive the image at the server:The corresponding code is shown below.

    void ClientSocket::readyRead()
    {

         ********** **  QByteArray buffer =  socket->readAll();**************
           qDebug() << "socket read data\n";
    
    
         QDataStream stream( buffer );
         stream.setVersion( QDataStream::Qt_5_5 );
         quint16 width, height, y;
         stream >> height >> width >> y;
        qDebug() << "crl1" << width << height;
    
         qDebug() << "socket read data 1\n";
    
    
         if( !image )
             image = new QImage( width, height, QImage::Format_RGB32 );
         else if( image->width() != width || image->height() != height )
         {
             delete image;
             image = new QImage( width, height, QImage::Format_RGB32 );
         }
    
         qDebug() << "socket read data 2\n";
    
         for( int x=0; x<width; ++x ) {
             for( int z=y;z<y+block_size;z++){
             quint8 red, green, blue;
             stream >> red >> green >> blue;
             image->setPixel( x, z, qRgb( red, green, blue ) );
             }
         }
    
     qDebug() << "socket read data 3\n";
    
     setPixmap( QPixmap::fromImage( *image ) );
    
     resize( image->size() );
    
     this->show();
    

    }

    I dont know the correct way of reading the incoming TCP packet.
    QByteArray buffer = socket->readAll();

    Should i communicate the block size of image and then read only those bytes at the server. How to do it? I have no idea.

    Hope some one will help me out.

    Warm Regards


  • Lifetime Qt Champion

    Hi and welcome to devnet,

    Take a look at the "Fortune Server Example" in Qt's documentation, you'll see how to send data through QTcpSocket with QDataStream.

    On a side note, you don't need to allocate QImage on the heap,


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.