Results 1 to 5 of 5

Thread: Send Image over TCP failed ??

  1. #1
    Join Date
    Sep 2015
    Posts
    23
    Thanks
    3
    Qt products
    Qt5
    Platforms
    Windows

    Default Send Image over TCP failed ??

    Hi,

    I am trying to send the image(768*1024) block by block ie, 768*20 blocks or more specifically stripe by stripe.

    The client sends such stripes of image and server displays the image.

    Here is the client code:

    client_image.h

    Qt Code:
    1. #ifndef CLIENT_IMAGE_H
    2. #define CLIENT_IMAGE_H
    3.  
    4. #include <QObject>
    5. #include <QLabel>
    6. #include <QImage>
    7. #include <QObject>
    8. #include <QThread>
    9. #include <QtNetwork/QTcpSocket>
    10. #include <QDebug>
    11. #include <QPixmap>
    12.  
    13.  
    14. class Client_Image:public QObject
    15.  
    16. {
    17. Q_OBJECT
    18. public:
    19. Client_Image();
    20. signals:
    21. void done();
    22.  
    23. private slots:
    24. void broadcastLine();
    25.  
    26. private:
    27. QTcpSocket *socket;
    28. QImage *image;
    29. quint16 a;
    30. quint16 img_block;
    31. quint8 block_size;
    32.  
    33. };
    34.  
    35. #endif // CLIENT_IMAGE_H
    To copy to clipboard, switch view to plain text mode 



    Client_Image Source Code ::::


    Qt Code:
    1. #include "client_image.h"
    2. #include <QDebug>
    3. #include <QObject>
    4. #include <QImage>
    5. #include <QTimer>
    6. #include <QByteArray>
    7. #include <QDataStream>
    8.  
    9.  
    10.  
    11.  
    12. Client_Image::Client_Image()
    13. {
    14.  
    15. socket = new QTcpSocket(this);
    16. a = 0;
    17. img_block = 0;
    18. block_size = 20;
    19.  
    20. socket->connectToHost("127.0.0.1",1234);
    21.  
    22. image = new QImage("C:/Users/Public/Pictures/Sample Pictures/Desert.jpg" );
    23.  
    24. if( image->isNull() )
    25. qDebug() << )"Failed to open test.jpg" ;
    26. else
    27. qDebug() << "Opened the test.jpg" ;
    28.  
    29. QTimer *timer = new QTimer(this );
    30. timer->isSingleShot();
    31. QTimer::singleShot(1000, this, SLOT(broadcastLine()) );
    32. connect( this, SIGNAL(done()), this, SLOT(broadcastLine()) );
    33.  
    34. }
    35.  
    36. void Client_Image::broadcastLine()
    37. {
    38.  
    39. QByteArray buffer( block_size+3*image->width(), 0 );
    40. QDataStream stream( &buffer, QIODevice::WriteOnly );
    41. stream.setVersion( QDataStream::Qt_5_5 );
    42.  
    43. stream << (quint16)image->width() << (quint16)image->height();
    44.  
    45.  
    46. stream << img_block;
    47.  
    48.  
    49. for( int x=0; x<image->width(); ++x )
    50. {
    51. for(int y=block_size*a; y <block_size+(block_size*a);y++)
    52. {
    53. if( y < image->height())
    54. {
    55. QRgb rgb = image->pixel( x, y );
    56. stream << (quint8)qRed( rgb ) << (quint8)qGreen( rgb ) << (quint8)qBlue( rgb );
    57. }
    58. else
    59. {
    60. y = a = 0;
    61. img_block = -block_size;
    62. }
    63. }
    64. }
    65.  
    66.  
    67. socket->write(buffer);
    68. qDebug() << buffer.size() << "buffer size";
    69.  
    70. socket->waitForBytesWritten(10);
    71. socket->flush();
    72. img_block = img_block + block_size;
    73. a++;
    74.  
    75. emit done();
    76.  
    77. }
    To copy to clipboard, switch view to plain text mode 






    Server Side Code used for Image display

    Xrayserver.h

    Qt Code:
    1. #ifndef XRAYSERVER_H
    2. #define XRAYSERVER_H
    3.  
    4. #include <QObject>
    5. #include <QTcpServer>
    6. #include "clientsocket.h"
    7. class XRayServer : public QTcpServer
    8. {
    9. Q_OBJECT
    10.  
    11. public:
    12.  
    13. explicit XRayServer(QObject *parent = 0);
    14. void startServer();
    15.  
    16. signals:
    17.  
    18.  
    19. public slots:
    20.  
    21.  
    22. protected:
    23. void incomingConnection(qintptr socketDescriptor);
    24.  
    25.  
    26. };
    27.  
    28. #endif // XRAYSERVER_H
    To copy to clipboard, switch view to plain text mode 


    Xrayserver.cpp

    Qt Code:
    1. #include "xrayserver.h"
    2. //#inlcude "clientsocket.h"
    3. #include<QThread>
    4.  
    5.  
    6. XRayServer::XRayServer(QObject *parent) : QTcpServer(parent)
    7. {
    8.  
    9. }
    10.  
    11. void XRayServer::startServer()
    12. {
    13. int port = 1234;
    14. if(!this->listen(QHostAddress::Any, port))
    15. {
    16. qDebug() << "Could not start server";
    17. }
    18. else
    19. {
    20. qDebug() << "Listening to port " << port << "...";
    21. }
    22. }
    23.  
    24.  
    25. void XRayServer::incomingConnection(qintptr socketDescriptor)
    26. {
    27. // We have a new connection
    28. qDebug() << socketDescriptor << " Connecting...";
    29.  
    30. QThread* client_thread = new QThread;
    31. ClientSocket* client_socket = new ClientSocket(socketDescriptor);
    32.  
    33.  
    34. }
    To copy to clipboard, switch view to plain text mode 


    One socket for each client connected to the server(Clientsocket.h and clientsocket.cpp files below)

    ClientSocket.h
    Qt Code:
    1. ClientSocket.h /// Header file
    2.  
    3. #ifndef CLIENTSOCKET_H
    4. #define CLIENTSOCKET_H
    5.  
    6. #include <QLabel>
    7. #include <QImage>
    8. #include <QObject>
    9. #include <QThread>
    10. #include <QTcpSocket>
    11. #include <QDebug>
    12. #include <QPixmap>
    13. #include <QHostAddress>
    14. class ClientSocket : public QLabel
    15. {
    16. Q_OBJECT
    17. public:
    18. explicit ClientSocket(qintptr ID);
    19.  
    20. signals:
    21. void error(QTcpSocket::SocketError socketerror);
    22. public slots:
    23. void readyRead();
    24. void disconnected();
    25. private:
    26. QTcpSocket *socket;
    27. qintptr socketDescriptor;
    28. QImage *image;
    29. quint8 block_size;
    30. QHostAddress ipaddress;
    31.  
    32. };
    33.  
    34. #endif // CLIENTSOCKET_H
    To copy to clipboard, switch view to plain text mode 

    ClientSocket.cpp
    Qt Code:
    1. #include "clientsocket.h"
    2. #include <QImage>
    3. #include <QPixmap>
    4. #include <QtNetwork/QTcpSocket>
    5. #include <QDataStream>
    6.  
    7. ClientSocket::ClientSocket(qintptr ID)
    8. {
    9.  
    10. socketDescriptor = ID;
    11. socket = new QTcpSocket();
    12. image = 0;
    13. block_size = 20;
    14. setMinimumSize(768,1024);
    15.  
    16. if(!socket->setSocketDescriptor(this->socketDescriptor))
    17. {
    18.  
    19. emit error(socket->error());
    20. return;
    21. }
    22.  
    23. QObject::connect(socket, SIGNAL(readyRead()), this, SLOT(readyRead()), Qt::DirectConnection);
    24. QObject::connect(socket, SIGNAL(disconnected()), this, SLOT(disconnected()));
    25. qDebug() << socketDescriptor << " Client connected";
    26. ipaddress = socket->peerAddress();
    27. qDebug() << ipaddress ;
    28.  
    29. }
    30.  
    31. void ClientSocket::readyRead()
    32. {
    33.  
    34. QByteArray buffer = socket->readAll();
    35. qDebug() << "socket read data\n";
    36.  
    37.  
    38. QDataStream stream( buffer );
    39. stream.setVersion( QDataStream::Qt_5_5 );
    40. quint16 width, height, y;
    41. stream >> height >> width >> y;
    42. qDebug() << "crl1" << width << height;
    43.  
    44. qDebug() << "socket read data 1\n";
    45.  
    46.  
    47. if( !image )
    48. image = new QImage( width, height, QImage::Format_RGB32 );
    49. else if( image->width() != width || image->height() != height )
    50. {
    51. delete image;
    52. image = new QImage( width, height, QImage::Format_RGB32 );
    53. }
    54.  
    55. qDebug() << "socket read data 2\n";
    56.  
    57. for( int x=0; x<width; ++x ) {
    58. for( int z=y;z<y+block_size;z++){
    59. quint8 red, green, blue;
    60. stream >> red >> green >> blue;
    61. image->setPixel( x, z, qRgb( red, green, blue ) );
    62. }
    63. }
    64.  
    65. qDebug() << "socket read data 3\n";
    66.  
    67. setPixmap( QPixmap::fromImage( *image ) );
    68.  
    69. resize( image->size() );
    70.  
    71. this->show();
    72. }
    73.  
    74.  
    75.  
    76. void ClientSocket::disconnected()
    77. {
    78. qDebug() << socketDescriptor << " Disconnected";
    79.  
    80. socket->deleteLater();
    81.  
    82. }
    To copy to clipboard, switch view to plain text mode 




    The above method worked for UDP sockets but not working for TCP.


    I know there is something wrong with socket->readAll() in void ClientSocket::readyRead() function in ClientSocket.cpp source file. But dont know how to correct it.

    Regards
    Last edited by ganapathi; 30th November 2015 at 07:28.

  2. #2
    Join Date
    Oct 2009
    Posts
    483
    Thanked 97 Times in 94 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Send Image over TCP failed ??

    You have not provided any details beyond "it does not work", so you will probably get vague answers.

    Your TCP code suffers from the same problems as your UDP code, which I pointed out in your other thread: strange global variables such as "a" and "img_block" are being modified all over the place, the "y" counter in a for loop is modified inside the body of the loop, etc. Maybe you got it working on UDP, but I wonder how maintainable it is.

    Here is one new problem in your TCP code: you seem to assume that after you call socket->write(buffer) in the client, the server receives all this data, and only it, as one nice block, which you can read with socket->readAll(). That is not how TCP works. I strongly suggest you read http://blog.stephencleary.com/2009/0...e-framing.html to understand the problem.

  3. #3
    Join Date
    Sep 2015
    Posts
    23
    Thanks
    3
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Send Image over TCP failed ??

    Quote Originally Posted by yeye_olive View Post

    Your TCP code suffers from the same problems as your UDP code, which I pointed out in your other thread: strange global variables such as "a" and "img_block" are being modified all over the place, the "y" counter in a for loop is modified inside the body of the loop, etc. Maybe you got it working on UDP, but I wonder how maintainable it is.

    Here is one new problem in your TCP code: you seem to assume that after you call socket->write(buffer) in the client, the server receives all this data, and only it, as one nice block, which you can read with socket->readAll(). That is not how TCP works. I strongly suggest you read http://blog.stephencleary.com/2009/0...e-framing.html to understand the problem.
    My aim is only to test the concepts than optimize it. Just get it working.

    I will go through that link and implement those solutions and let you know further .

    Thanks a lot for the link.

  4. #4
    Join Date
    Jan 2006
    Location
    Sta. Eugènia de Berga (Vic - Barcelona - Spain)
    Posts
    869
    Thanks
    70
    Thanked 59 Times in 57 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Send Image over TCP failed ??

    Hi,

    As yeye_olive explained, you are assuming that the full image will be received in one slot call on the client side. This is not true, so you will receive the image into blocks(data chunks).

    My aim is only to test the concepts than optimize it. Just get it working.
    This is not a optimization problem, the problem is the concept.

    Please, read the link that yeye_olive provided you.
    Òscar Llarch i Galán

  5. #5
    Join Date
    Sep 2015
    Posts
    23
    Thanks
    3
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Send Image over TCP failed ??

    Quote Originally Posted by yeye_olive View Post
    You have not provided any details beyond "it does not work", so you will probably get vague answers.

    Your TCP code suffers from the same problems as your UDP code, which I pointed out in your other thread: strange global variables such as "a" and "img_block" are being modified all over the place, the "y" counter in a for loop is modified inside the body of the loop, etc. Maybe you got it working on UDP, but I wonder how maintainable it is.

    Here is one new problem in your TCP code: you seem to assume that after you call socket->write(buffer) in the client, the server receives all this data, and only it, as one nice block, which you can read with socket->readAll(). That is not how TCP works. I strongly suggest you read http://blog.stephencleary.com/2009/0...e-framing.html to understand the problem.
    Dear yeye_olive,

    I implemented http://blog.stephencleary.com/2009/0...e-framing.html length prefixing solution and is working perfect. I will share the code once fully implemented.

    Nothing is global. Server and Clent are in different classes and no sharing at all.
    Regarding the y variable modification , I will check out once more.

    Thanks Yeye

Similar Threads

  1. QML Image: Failed to get image from provider
    By mismael85 in forum Qt Quick
    Replies: 1
    Last Post: 13th December 2012, 15:37
  2. How to send Image trough TCP/ip in Qt?
    By qtlinuxnewbie in forum Newbie
    Replies: 5
    Last Post: 27th April 2010, 03:21
  3. Release program failed to show jpg image on another PC!
    By bangqianchen in forum Qt Programming
    Replies: 4
    Last Post: 31st January 2010, 22:16
  4. Saving image in jpeg format failed
    By febil in forum Qt Programming
    Replies: 5
    Last Post: 23rd April 2009, 11:33
  5. Send a Image throw a QDataStream
    By gt.beta2 in forum Newbie
    Replies: 5
    Last Post: 20th March 2009, 23:07

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.