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.
  • mandruk1331M mandruk1331

    I have a client server architecture, the client can connect to server side. I have already checked that and it also writes data to the socket, but on the server side the ReadyRead signal is not being emitted, can someone help me out? thank you

    #include "tcpsocket.h"
    #define PORT 7755
    #define LOWER_BIT 0
    TCPSocket::TCPSocket()
    {
    
      //  socket = new QTcpSocket(this);
        connect(&server,SIGNAL(newConnection()),this,SLOT(newConnectionS()));
    
    }
    
    void TCPSocket::ListenS(){
        if(server.listen(QHostAddress::LocalHost,5555)){
            qDebug()<<"Server is listening";
        }
        else{
            qDebug()<<"Server cannot be started"<<server.errorString();
        }
    }
    void TCPSocket::newConnectionS(){
        qDebug()<<"Connected";
         socket = server.nextPendingConnection();
        socket->open(QIODevice::ReadWrite);
        if(socket->isOpen()){
            qDebug()<<"Socket is open";
        }else {qDebug()<<"Socket cannot be opened"<<socket->errorString();}
    
        connect(socket,SIGNAL(readyRead()),this,SLOT(newImageReceived()));
    }
    
    void TCPSocket::newImageReceived()
    {
        qDebug()<<"Img rece";
        QByteArray ba;
        if(socket->bytesAvailable() <=0){
            qDebug()<<"Cannot read from socket"<<socket->errorString();
        }else{
        ba = socket->readAll();
        }
        QImage img = QImage::fromData(ba);
        emit sendImage(img);
    }
    
    K Offline
    K Offline
    koahnig
    wrote on last edited by
    #2

    @mandruk1331

    As already written in your other thread, it is a good idea to check out one of the examples (e.g. the fortune server/client example). There you can the different steps for handling a server client connection. Here is the example for the server part Checkout where nextPendingConnection is used.

    You need to remove the open statement and it should work:

    void TCPSocket::newConnectionS(){
        qDebug()<<"Connected";
         socket = server.nextPendingConnection();
        //  socket->open(QIODevice::ReadWrite); <== this is not required
        if(socket->isOpen()){
            qDebug()<<"Socket is open";
        }else {qDebug()<<"Socket cannot be opened"<<socket->errorString();}
    
        connect(socket,SIGNAL(readyRead()),this,SLOT(newImageReceived()));
    }
    

    Vote the answer(s) that helped you to solve your issue(s)

    mandruk1331M 1 Reply Last reply
    1
    • K koahnig

      @mandruk1331

      As already written in your other thread, it is a good idea to check out one of the examples (e.g. the fortune server/client example). There you can the different steps for handling a server client connection. Here is the example for the server part Checkout where nextPendingConnection is used.

      You need to remove the open statement and it should work:

      void TCPSocket::newConnectionS(){
          qDebug()<<"Connected";
           socket = server.nextPendingConnection();
          //  socket->open(QIODevice::ReadWrite); <== this is not required
          if(socket->isOpen()){
              qDebug()<<"Socket is open";
          }else {qDebug()<<"Socket cannot be opened"<<socket->errorString();}
      
          connect(socket,SIGNAL(readyRead()),this,SLOT(newImageReceived()));
      }
      
      mandruk1331M Offline
      mandruk1331M Offline
      mandruk1331
      wrote on last edited by mandruk1331
      #3

      @koahnig It did not help(
      I know that I asked this question already but now the it does not work, it does not even send a simple message, because the ReadyRead signal is not being emitted. On the client side the data is being written to the socket because it returns the size of the data being written into the socket. Maybe smth wrong with the client side:

      #include "tcpsocket.h"
      #define PORT 7755
      TCPSocket::TCPSocket()
      {
       socket = new QTcpSocket(this);
       connect(socket,SIGNAL(bytesWritten(qint64)),this,SLOT(bytesWrittenC(qint64)));
      }
      void TCPSocket::bytesWrittenC(qint64 b){
          qDebug()<<"Data is ready:"<<b;
      }
      void TCPSocket::ReadyReadC(){
          qDebug()<<"Ready read";
          qDebug()<<"Info: "<<socket->readAll();
      }
      
      void TCPSocket::SendData(QImage &frame){
          QByteArray ba;
          QBuffer buffer(&ba);
          buffer.open(QIODevice::ReadWrite);
          frame.save(&buffer, "jpg");
          //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(buffer.data());
          qDebug()<<"Bytes available"<<socket->bytesAvailable();
      
      
      }
      

      Mandruk1331

      K 1 Reply Last reply
      0
      • mandruk1331M mandruk1331

        @koahnig It did not help(
        I know that I asked this question already but now the it does not work, it does not even send a simple message, because the ReadyRead signal is not being emitted. On the client side the data is being written to the socket because it returns the size of the data being written into the socket. Maybe smth wrong with the client side:

        #include "tcpsocket.h"
        #define PORT 7755
        TCPSocket::TCPSocket()
        {
         socket = new QTcpSocket(this);
         connect(socket,SIGNAL(bytesWritten(qint64)),this,SLOT(bytesWrittenC(qint64)));
        }
        void TCPSocket::bytesWrittenC(qint64 b){
            qDebug()<<"Data is ready:"<<b;
        }
        void TCPSocket::ReadyReadC(){
            qDebug()<<"Ready read";
            qDebug()<<"Info: "<<socket->readAll();
        }
        
        void TCPSocket::SendData(QImage &frame){
            QByteArray ba;
            QBuffer buffer(&ba);
            buffer.open(QIODevice::ReadWrite);
            frame.save(&buffer, "jpg");
            //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(buffer.data());
            qDebug()<<"Bytes available"<<socket->bytesAvailable();
        
        
        }
        
        K Offline
        K Offline
        koahnig
        wrote on last edited by
        #4

        @mandruk1331

        Same thing here, comment out socket->open(QIODevice::ReadWrite);

        void TCPSocket::SendData(QImage &frame){
            QByteArray ba;
            QBuffer buffer(&ba);
            buffer.open(QIODevice::ReadWrite);
            frame.save(&buffer, "jpg");
            //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); <=== comment out 
            qDebug()<<"Data written"<<socket->write(buffer.data());
            qDebug()<<"Bytes available"<<socket->bytesAvailable();
        }
        

        You open a device which is already open. See connectToHost
        It says in second paragraph:
        The socket is opened in the given openMode and first enters HostLookupState, then performs a host ...

        Vote the answer(s) that helped you to solve your issue(s)

        mandruk1331M 1 Reply Last reply
        0
        • K koahnig

          @mandruk1331

          Same thing here, comment out socket->open(QIODevice::ReadWrite);

          void TCPSocket::SendData(QImage &frame){
              QByteArray ba;
              QBuffer buffer(&ba);
              buffer.open(QIODevice::ReadWrite);
              frame.save(&buffer, "jpg");
              //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); <=== comment out 
              qDebug()<<"Data written"<<socket->write(buffer.data());
              qDebug()<<"Bytes available"<<socket->bytesAvailable();
          }
          

          You open a device which is already open. See connectToHost
          It says in second paragraph:
          The socket is opened in the given openMode and first enters HostLookupState, then performs a host ...

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

          @koahnig I have commented that, but it stills does not work, the client connects to the serverm but the server does not emit the ReadyRead signal.Also tried to connect to the server part with telnet, it does connect and sends chars to the server and the server part emits the ReadyRead signal. Can the problem be that the client part writes the data way too fast to the socket and the server side does not have the time to read from it?
          Console output: (Client side):

          QObject: Cannot create children for a parent that is in a different thread.
          (Parent is QTcpSocket(0x20ea3780), parent's thread is QThread(0x20546478), current thread is _Camera(0x20e42d30)
          QObject: Cannot create children for a parent that is in a different thread.
          (Parent is QTcpSocket(0x20ea3780), parent's thread is QThread(0x20546478), current thread is _Camera(0x20e42d30)
          Socket is valid
          Data written 15063
          Bytes available 0
          Socket is valid
          Data written 15274
          Bytes available 0
          

          Mandruk1331

          kshegunovK 1 Reply Last reply
          0
          • mandruk1331M mandruk1331

            @koahnig I have commented that, but it stills does not work, the client connects to the serverm but the server does not emit the ReadyRead signal.Also tried to connect to the server part with telnet, it does connect and sends chars to the server and the server part emits the ReadyRead signal. Can the problem be that the client part writes the data way too fast to the socket and the server side does not have the time to read from it?
            Console output: (Client side):

            QObject: Cannot create children for a parent that is in a different thread.
            (Parent is QTcpSocket(0x20ea3780), parent's thread is QThread(0x20546478), current thread is _Camera(0x20e42d30)
            QObject: Cannot create children for a parent that is in a different thread.
            (Parent is QTcpSocket(0x20ea3780), parent's thread is QThread(0x20546478), current thread is _Camera(0x20e42d30)
            Socket is valid
            Data written 15063
            Bytes available 0
            Socket is valid
            Data written 15274
            Bytes available 0
            
            kshegunovK Offline
            kshegunovK Offline
            kshegunov
            Moderators
            wrote on last edited by
            #6

            @mandruk1331 said in Server side does not emit ReadyRead signal:

            QObject: Cannot create children for a parent that is in a different thread.
            (Parent is QTcpSocket(0x20ea3780), parent's thread is QThread(0x20546478), current thread is _Camera(0x20e42d30)

            Don't you consider this to be worthy of fixing?

            Read and abide by the Qt Code of Conduct

            mandruk1331M 1 Reply Last reply
            3
            • kshegunovK kshegunov

              @mandruk1331 said in Server side does not emit ReadyRead signal:

              QObject: Cannot create children for a parent that is in a different thread.
              (Parent is QTcpSocket(0x20ea3780), parent's thread is QThread(0x20546478), current thread is _Camera(0x20e42d30)

              Don't you consider this to be worthy of fixing?

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

              @kshegunov I have fixed the issue with the thread, but still the signal is not being emitted on the server side, I just don't understand what is wrong. For getting the video from the webcam I use OpenCV.

              Mandruk1331

              jsulmJ kshegunovK 2 Replies Last reply
              0
              • mandruk1331M mandruk1331

                @kshegunov I have fixed the issue with the thread, but still the signal is not being emitted on the server side, I just don't understand what is wrong. For getting the video from the webcam I use OpenCV.

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

                @mandruk1331 Are there any other warnings when running the client or server?
                Also: before writing to socket you should either call http://doc.qt.io/qt-5/qabstractsocket.html#waitForConnected or connect slot to http://doc.qt.io/qt-5/qabstractsocket.html#connected signal and write in this slot to make sure you're actually connected at the time you're trying to write. And you should always connect slots to error signals.

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

                mandruk1331M 1 Reply Last reply
                1
                • mandruk1331M mandruk1331

                  @kshegunov I have fixed the issue with the thread, but still the signal is not being emitted on the server side, I just don't understand what is wrong. For getting the video from the webcam I use OpenCV.

                  kshegunovK Offline
                  kshegunovK Offline
                  kshegunov
                  Moderators
                  wrote on last edited by
                  #9

                  Start by making sure you're sending data. Test your client by sending a few bytes to a server application that's known to work, e.g. the fortune server Qt example. Then test your server, for example open netcat and send a few bytes manually to your server, if you receive them, then the problem is most certainly with your client. Also make sure you're not using threading on the server, as QTcpServer::nextPendingConnection is not the correct way to do it.

                  Read and abide by the Qt Code of Conduct

                  1 Reply Last reply
                  0
                  • BjornWB Offline
                    BjornWB Offline
                    BjornW
                    wrote on last edited by
                    #10
                    if(connectedToHostC == false){
                        socket->connectToHost(QHostAddress::LocalHost,PORT,QIODevice::ReadWrite);
                         connect(socket,SIGNAL(readyRead()),this,SLOT(ReadyReadC()));
                        connectedToHostC = true;
                        }
                    

                    Can you verify that the socket actually connects?

                    qDebug() << socket->state();
                    
                    1 Reply Last reply
                    0
                    • jsulmJ jsulm

                      @mandruk1331 Are there any other warnings when running the client or server?
                      Also: before writing to socket you should either call http://doc.qt.io/qt-5/qabstractsocket.html#waitForConnected or connect slot to http://doc.qt.io/qt-5/qabstractsocket.html#connected signal and write in this slot to make sure you're actually connected at the time you're trying to write. And you should always connect slots to error signals.

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

                      @jsulm , @kshegunov @BjornW At the moment the photo is being transmitted to the server part. And when the frame is showed on the server part the lower part of the image is grey don't really know why and now when I want to scale it so it would fit the label size it gives these errors in the console:

                      QImage::scaled: Image is a null image
                      Img rece
                      Corrupt JPEG data: premature end of data segment
                      

                      Mandruk1331

                      jsulmJ 1 Reply Last reply
                      0
                      • mandruk1331M mandruk1331

                        @jsulm , @kshegunov @BjornW At the moment the photo is being transmitted to the server part. And when the frame is showed on the server part the lower part of the image is grey don't really know why and now when I want to scale it so it would fit the label size it gives these errors in the console:

                        QImage::scaled: Image is a null image
                        Img rece
                        Corrupt JPEG data: premature end of data segment
                        
                        jsulmJ Offline
                        jsulmJ Offline
                        jsulm
                        Lifetime Qt Champion
                        wrote on last edited by jsulm
                        #12

                        @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.

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

                        mandruk1331M 1 Reply Last reply
                        2
                        • 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

                                          • Login

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