Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Correct way to read TCP packet :socket->readAll()?
Forum Updated to NodeBB v4.3 + New Features

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

Scheduled Pinned Locked Moved Unsolved General and Desktop
2 Posts 2 Posters 5.8k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Offline
    G Offline
    ganapathi
    wrote on 30 Nov 2015, 08:30 last edited by ganapathi
    #1

    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

    1 Reply Last reply
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 2 Dec 2015, 22:04 last edited by
      #2

      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,

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0

      1/2

      30 Nov 2015, 08:30

      • Login

      • Login or register to search.
      1 out of 2
      • First post
        1/2
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved