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. Server side does not emit ReadyRead signal
Qt 6.11 is out! See what's new in the release blog

Server side does not emit ReadyRead signal

Scheduled Pinned Locked Moved Solved General and Desktop
26 Posts 5 Posters 11.8k Views 2 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.
  • jsulmJ jsulm

    @mandruk1331 As the error message says your image is corrupted (most probably incomplete). You should check your networking code.
    How do you read the data on the server? Keep in mind that most probably the data does not arrive at once but in several chunks.

    mandruk1331M Offline
    mandruk1331M Offline
    mandruk1331
    wrote on last edited by mandruk1331
    #13

    @jsulm On the server side I use the readAll function like this:
    QString str(socket->readAll());
    And how I can make the client side to wait for a msg from the server side. The question how I can make TCP/IP synchronous?
    If you want I can give a link to GitHub account.

    Mandruk1331

    jsulmJ 1 Reply Last reply
    0
    • mandruk1331M mandruk1331

      @jsulm On the server side I use the readAll function like this:
      QString str(socket->readAll());
      And how I can make the client side to wait for a msg from the server side. The question how I can make TCP/IP synchronous?
      If you want I can give a link to GitHub account.

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by jsulm
      #14

      @mandruk1331

      1. Why do you convert the data to QString?! You expect to get an JPEG image, right?
      2. Do you call readAll() only once? As I said the data arrives in several chunks and you need to read all of them. You connect readyRead signal to a slot and in this slot you call readAll() and store the data in a buffer until you have all the data.

      "And how I can make the client side to wait for a msg from the server side" - what do you mean? you send a picture from client to server, right?
      "The question how I can make TCP/IP synchronous?" - you don't. Why would you?

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      mandruk1331M 1 Reply Last reply
      2
      • jsulmJ jsulm

        @mandruk1331

        1. Why do you convert the data to QString?! You expect to get an JPEG image, right?
        2. Do you call readAll() only once? As I said the data arrives in several chunks and you need to read all of them. You connect readyRead signal to a slot and in this slot you call readAll() and store the data in a buffer until you have all the data.

        "And how I can make the client side to wait for a msg from the server side" - what do you mean? you send a picture from client to server, right?
        "The question how I can make TCP/IP synchronous?" - you don't. Why would you?

        mandruk1331M Offline
        mandruk1331M Offline
        mandruk1331
        wrote on last edited by
        #15

        @jsulm From the server side I expect a QString "1" img passed the validation "0" !passed. I call it only once. How I can determine that the buffer has all the data that can be used?
        Client->JPEG->Server (checks if img is ok)->send msg that img is ok->client rcv that img is ok.

        Mandruk1331

        jsulmJ 1 Reply Last reply
        0
        • mandruk1331M mandruk1331

          @jsulm From the server side I expect a QString "1" img passed the validation "0" !passed. I call it only once. How I can determine that the buffer has all the data that can be used?
          Client->JPEG->Server (checks if img is ok)->send msg that img is ok->client rcv that img is ok.

          jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote on last edited by
          #16

          @mandruk1331 "From the server side I expect a QString "1" img passed the validation "0" !passed" - actually I wanted to know how you read on server.
          You need to define a protocol. For example the client could send first an int containing the number of bytes it is going to send next. Then the server knows when all data was received.

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          mandruk1331M 1 Reply Last reply
          2
          • jsulmJ jsulm

            @mandruk1331 "From the server side I expect a QString "1" img passed the validation "0" !passed" - actually I wanted to know how you read on server.
            You need to define a protocol. For example the client could send first an int containing the number of bytes it is going to send next. Then the server knows when all data was received.

            mandruk1331M Offline
            mandruk1331M Offline
            mandruk1331
            wrote on last edited by mandruk1331
            #17

            @jsulm At the moment I can send a videostream over the socket to the server side using TCP/IP protocol and I was a little bit surprised because it works really really good, the frame rate on the server side is the same as on the client side. Now I will try to send the validation string from the server side to the client. A big thank you for everybody who gave me the advice on how to fix my problem and a special thank you to JSULM.

            Mandruk1331

            jsulmJ 1 Reply Last reply
            0
            • mandruk1331M mandruk1331

              @jsulm At the moment I can send a videostream over the socket to the server side using TCP/IP protocol and I was a little bit surprised because it works really really good, the frame rate on the server side is the same as on the client side. Now I will try to send the validation string from the server side to the client. A big thank you for everybody who gave me the advice on how to fix my problem and a special thank you to JSULM.

              jsulmJ Offline
              jsulmJ Offline
              jsulm
              Lifetime Qt Champion
              wrote on last edited by
              #18

              @mandruk1331 Glad I could help :-)

              https://forum.qt.io/topic/113070/qt-code-of-conduct

              mandruk1331M 1 Reply Last reply
              0
              • jsulmJ jsulm

                @mandruk1331 Glad I could help :-)

                mandruk1331M Offline
                mandruk1331M Offline
                mandruk1331
                wrote on last edited by
                #19

                @jsulm One more question,. At the moment the frame are being transmitted between client and server, but on the server side I get a corrupted image (lower part of it is grey), but if the image size is less then 9k bytes than everything is ok, how I can make the image to be transmitted fully?
                Thank you.

                Mandruk1331

                jsulmJ 1 Reply Last reply
                0
                • mandruk1331M mandruk1331

                  @jsulm One more question,. At the moment the frame are being transmitted between client and server, but on the server side I get a corrupted image (lower part of it is grey), but if the image size is less then 9k bytes than everything is ok, how I can make the image to be transmitted fully?
                  Thank you.

                  jsulmJ Offline
                  jsulmJ Offline
                  jsulm
                  Lifetime Qt Champion
                  wrote on last edited by
                  #20

                  @mandruk1331 I explained it above: you have to make sure you read the whole picture on the server side. Data is transmitted in packages over the network - you don't know how many packages you will get, so you need to read until you got everything.
                  One question: how big are the broken image on the server side? Do they have exact same size as on client side?

                  https://forum.qt.io/topic/113070/qt-code-of-conduct

                  mandruk1331M 1 Reply Last reply
                  1
                  • jsulmJ jsulm

                    @mandruk1331 I explained it above: you have to make sure you read the whole picture on the server side. Data is transmitted in packages over the network - you don't know how many packages you will get, so you need to read until you got everything.
                    One question: how big are the broken image on the server side? Do they have exact same size as on client side?

                    mandruk1331M Offline
                    mandruk1331M Offline
                    mandruk1331
                    wrote on last edited by
                    #21

                    @jsulm So I can write to the socket the size of the image therefore the server will know the size of the picture, and after that send the image?

                    Mandruk1331

                    jsulmJ 1 Reply Last reply
                    0
                    • mandruk1331M mandruk1331

                      @jsulm So I can write to the socket the size of the image therefore the server will know the size of the picture, and after that send the image?

                      jsulmJ Offline
                      jsulmJ Offline
                      jsulm
                      Lifetime Qt Champion
                      wrote on last edited by
                      #22

                      @mandruk1331 yes

                      https://forum.qt.io/topic/113070/qt-code-of-conduct

                      mandruk1331M 2 Replies Last reply
                      0
                      • jsulmJ jsulm

                        @mandruk1331 yes

                        mandruk1331M Offline
                        mandruk1331M Offline
                        mandruk1331
                        wrote on last edited by
                        #23

                        @jsulm Ok. Got it.

                        Mandruk1331

                        1 Reply Last reply
                        0
                        • jsulmJ jsulm

                          @mandruk1331 yes

                          mandruk1331M Offline
                          mandruk1331M Offline
                          mandruk1331
                          wrote on last edited by mandruk1331
                          #24

                          @jsulm so this if what I got so far
                          The client side send the size but the server does not response that he received the data, the string is "" but it has to be "2".
                          Could you tell me what am I doing wrong?
                          Server side:

                          void TCPSocket::newImageReceived()
                          {
                              qDebug()<<"Img rece";
                              QByteArray ba;
                              if(socket->bytesAvailable() <=0){
                                  qDebug()<<"Cannot read from socket"<<socket->errorString();
                              }else{
                                  if(socket->bytesAvailable()>0){
                                    QString size(socket->readAll());
                                    size_data = size.toInt();
                                    socket->write("2");
                                    socket->waitForBytesWritten(1000);
                                  }
                              }
                              while(size_data >0){
                                  qDebug()<<"Server:"<<socket->bytesAvailable();
                                  ba += socket->readAll();
                                  socket->waitForReadyRead(1000);
                                  size_data-=socket->bytesAvailable();
                              }
                              QImage img = QImage::fromData(ba);
                              //socket->waitForReadyRead(1000);
                              if(!img.isNull()){
                              if(checkFrame(img)){
                              //socket->write("1");
                              qDebug()<<"Frame ok";
                              }else{
                                  qDebug()<<"Frame not ok";
                                  //socket->write("0");
                              }
                              emit sendImage(img); // to the GUI side not over the network
                              }
                          
                          }
                          

                          Client side:

                          void _Camera::SendData(QImage &frame){
                              QByteArray ba;
                              QBuffer buffer(&ba);
                              buffer.open(QIODevice::ReadWrite);
                              frame.save(&buffer, "JPEG");
                              //ba.append("We did it");
                              //socket.setOpenMode(QIODevice::ReadWrite);
                              if(connectedToHostC == false){
                              socket.connectToHost(QHostAddress::LocalHost,PORT,QIODevice::ReadWrite);
                               connect(&socket,SIGNAL(readyRead()),this,SLOT(ReadyReadC()));
                              connectedToHostC = true;
                              }
                              if(socket.isValid()){
                                  qDebug()<<"Socket is valid";
                              }
                              //socket->open(QIODevice::ReadWrite);
                              qDebug()<<"Data written"<<socket.write(QString::number(buffer.size()).toStdString().c_str());
                              qDebug()<<"Bytes written"<<socket.waitForBytesWritten(1000);
                              while(socket.bytesAvailable() <0){
                              qDebug()<<"no data to be read";
                              }
                              QString response (socket.readAll());
                              qDebug()<<"response is"<<response;
                              if(response == "2"){
                                  qDebug()<<"Data written"<<socket.write(buffer.data());
                                  qDebug()<<"Bytes written"<<socket.waitForBytesWritten(1000);
                                  qDebug()<<"Bytes available"<<socket.bytesAvailable();
                              }else{
                                  qDebug()<<"smth is wrong";
                              }
                          }
                          

                          Mandruk1331

                          jsulmJ 1 Reply Last reply
                          0
                          • mandruk1331M mandruk1331

                            @jsulm so this if what I got so far
                            The client side send the size but the server does not response that he received the data, the string is "" but it has to be "2".
                            Could you tell me what am I doing wrong?
                            Server side:

                            void TCPSocket::newImageReceived()
                            {
                                qDebug()<<"Img rece";
                                QByteArray ba;
                                if(socket->bytesAvailable() <=0){
                                    qDebug()<<"Cannot read from socket"<<socket->errorString();
                                }else{
                                    if(socket->bytesAvailable()>0){
                                      QString size(socket->readAll());
                                      size_data = size.toInt();
                                      socket->write("2");
                                      socket->waitForBytesWritten(1000);
                                    }
                                }
                                while(size_data >0){
                                    qDebug()<<"Server:"<<socket->bytesAvailable();
                                    ba += socket->readAll();
                                    socket->waitForReadyRead(1000);
                                    size_data-=socket->bytesAvailable();
                                }
                                QImage img = QImage::fromData(ba);
                                //socket->waitForReadyRead(1000);
                                if(!img.isNull()){
                                if(checkFrame(img)){
                                //socket->write("1");
                                qDebug()<<"Frame ok";
                                }else{
                                    qDebug()<<"Frame not ok";
                                    //socket->write("0");
                                }
                                emit sendImage(img); // to the GUI side not over the network
                                }
                            
                            }
                            

                            Client side:

                            void _Camera::SendData(QImage &frame){
                                QByteArray ba;
                                QBuffer buffer(&ba);
                                buffer.open(QIODevice::ReadWrite);
                                frame.save(&buffer, "JPEG");
                                //ba.append("We did it");
                                //socket.setOpenMode(QIODevice::ReadWrite);
                                if(connectedToHostC == false){
                                socket.connectToHost(QHostAddress::LocalHost,PORT,QIODevice::ReadWrite);
                                 connect(&socket,SIGNAL(readyRead()),this,SLOT(ReadyReadC()));
                                connectedToHostC = true;
                                }
                                if(socket.isValid()){
                                    qDebug()<<"Socket is valid";
                                }
                                //socket->open(QIODevice::ReadWrite);
                                qDebug()<<"Data written"<<socket.write(QString::number(buffer.size()).toStdString().c_str());
                                qDebug()<<"Bytes written"<<socket.waitForBytesWritten(1000);
                                while(socket.bytesAvailable() <0){
                                qDebug()<<"no data to be read";
                                }
                                QString response (socket.readAll());
                                qDebug()<<"response is"<<response;
                                if(response == "2"){
                                    qDebug()<<"Data written"<<socket.write(buffer.data());
                                    qDebug()<<"Bytes written"<<socket.waitForBytesWritten(1000);
                                    qDebug()<<"Bytes available"<<socket.bytesAvailable();
                                }else{
                                    qDebug()<<"smth is wrong";
                                }
                            }
                            
                            jsulmJ Offline
                            jsulmJ Offline
                            jsulm
                            Lifetime Qt Champion
                            wrote on last edited by
                            #25

                            @mandruk1331 No need to send anything to the client after reading size. Also, you're reading size wrongly: size should have a fixed size (4 bytes should be enough), so you need to read exactly 4 bytes not a string which can have arbitrary size.
                            Then, remove this while loop where you read the picture data - this should be done in the usual way using readyRead signal. You really should take a look at the networking Qt examples.

                            https://forum.qt.io/topic/113070/qt-code-of-conduct

                            mandruk1331M 1 Reply Last reply
                            0
                            • jsulmJ jsulm

                              @mandruk1331 No need to send anything to the client after reading size. Also, you're reading size wrongly: size should have a fixed size (4 bytes should be enough), so you need to read exactly 4 bytes not a string which can have arbitrary size.
                              Then, remove this while loop where you read the picture data - this should be done in the usual way using readyRead signal. You really should take a look at the networking Qt examples.

                              mandruk1331M Offline
                              mandruk1331M Offline
                              mandruk1331
                              wrote on last edited by
                              #26

                              @jsulm Ok, I will take a look at the examples and the docs.

                              Mandruk1331

                              1 Reply Last reply
                              0

                              • Login

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