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. QUdpSocket does not trigger ReadyRead signal
Forum Updated to NodeBB v4.3 + New Features

QUdpSocket does not trigger ReadyRead signal

Scheduled Pinned Locked Moved Unsolved General and Desktop
26 Posts 6 Posters 5.3k 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.
  • O Offline
    O Offline
    onurcevik
    wrote on last edited by onurcevik
    #1

    I am reading and sending QImage with QUdpSocket in a for loop but I can't get the QImages because readyRead() signal doesn't get emmited. Here is my code:

    udpsocket.h

    #ifndef UDPSOCKET_H
    #define UDPSOCKET_H
    
    #include <QObject>
    #include <QUdpSocket>
    
    #include "camera.h"
    
    class UDPSocket : public QObject
    {
        Q_OBJECT
    public:
        explicit UDPSocket(QObject *parent = nullptr);
        void sendFrame(QImage frame);
    
    signals:
    
    
    public slots:
        void readyRead();
    
    private:
    
        QUdpSocket *socket;
    
    
    };
    
    #endif // UDPSOCKET_H
    
    

    udpsocket.cpp

     #include "udpsocket.h"
    #include <QDebug>
    #include <iostream>
    #include <fstream>
    #include <string>
    #include <sstream>
    #include <unistd.h>
    #include <udpsocket.h>
    #include <QBuffer>
    #include <QDebug>
    #include <QImage>
    #include <QImageWriter>
    
    #define XRES 640
    #define YRES 480
    #define SIZE 640*480*3
    
    
    
    UDPSocket::UDPSocket(QObject *parent) : QObject(parent)
    {
    
        socket = new QUdpSocket(this);
        bool deneme= socket->bind(QHostAddress::LocalHost,1234);
        if(deneme)
            qDebug()<<"binded";
        connect(socket,SIGNAL(readyRead()),this,SLOT(readyRead()));
    
    }
    
    
    void UDPSocket::sendFrame(QImage image)
    {
        qDebug()<<"will send";
    
        QBuffer buffer;
        QImageWriter writer(&buffer,"bmp");
        writer.write(image);
        QByteArray data;
        data.append(buffer.data());
        socket->writeDatagram(data,QHostAddress::Broadcast,1234);
    
    
        qDebug()<<"sent";
    
    
    }
    
    void UDPSocket::readyRead()
    {
        qDebug()<<"readyread";
        QByteArray buffer;
        buffer.resize(socket->pendingDatagramSize());
    
        QHostAddress sender;
        quint16 senderPort;
    
        socket->readDatagram(buffer.data(),buffer.size(),&sender,&senderPort);
    
    
        std::ostringstream stream;
        stream << "frames/" << qrand() << ".bmp";
        std::string new_string = stream.str();
        std::ofstream image;
        image.open(new_string);
        image << "P6\n" << XRES << " " << YRES << " 255\n";
    
        image.write(buffer.data(), SIZE);
        image.close();
        qDebug()<<"image arrived";
    }
    

    main.cpp

    #include <QCoreApplication>
    #include <camera.h>
    #include <QThread>
    #include <QDebug>
    #include <iostream>
    #include <fstream>
    #include <string>
    #include <sstream>
    #include <unistd.h>
    #include <udpsocket.h>
    
    #include <QPixmap>
    
    
    #define XRES 640
    #define YRES 480
    
    int main(int argc, char *argv[])
    {
        QCoreApplication a(argc, argv);
        UDPSocket udpObject;
    
        QImage *image;
    
         Camera camera("/dev/video0", XRES, YRES);
    
            for(int i=0; i<10; i++)
            {
               auto frame = camera.frame();
               QThread::sleep(0.0416);
               image = new QImage(frame.data,XRES,YRES,QImage::Format_RGB888);
               udpObject.sendFrame(*image);
    
    
    
    }
    
    
        return a.exec();
    }
    
    

    When I run this code I see

    binded
    will send
    sent
    

    10 times (because of for loop in main.cpp) and when I debug readyread() doesn't even run.

    How can I fix this code so I can recieve the images I sent ? Should I use connectToHost instead of bind() ?

    Note:camera.h is my class that I use to read images from camera and probably has nothing to do with error.

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      Are you trying to send and receive the image using the exact same socket ?

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

      O 1 Reply Last reply
      0
      • SGaistS SGaist

        Hi,

        Are you trying to send and receive the image using the exact same socket ?

        O Offline
        O Offline
        onurcevik
        wrote on last edited by
        #3

        @SGaist In main.cpp I create the UDPSocket object outside the for loop then use the same object to access the sendFrame(QImage frame); function to send the sockets inside the loop . So I guess I use the same socket.

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Then show what you are doing.

          From what you wrote, you don't even have an event loop running hence not signals/slots working.

          Also, why are you trying to use UPD to send data from your application back to the same application ?

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

          O 1 Reply Last reply
          2
          • SGaistS SGaist

            Then show what you are doing.

            From what you wrote, you don't even have an event loop running hence not signals/slots working.

            Also, why are you trying to use UPD to send data from your application back to the same application ?

            O Offline
            O Offline
            onurcevik
            wrote on last edited by
            #5

            @SGaist I already posted almost all of my code? What do you mean "show what you are doing?"
            I am sending/recieving on same PC to test if I can so later I can implement it for different PC's. Since Im newbie I dont know much about event loops can you show some example?

            jsulmJ 1 Reply Last reply
            0
            • O onurcevik

              @SGaist I already posted almost all of my code? What do you mean "show what you are doing?"
              I am sending/recieving on same PC to test if I can so later I can implement it for different PC's. Since Im newbie I dont know much about event loops can you show some example?

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

              @onurcevik Can you show your main.cpp?

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

              O 1 Reply Last reply
              0
              • jsulmJ jsulm

                @onurcevik Can you show your main.cpp?

                O Offline
                O Offline
                onurcevik
                wrote on last edited by
                #7

                @jsulm I edited and added main.cpp to my question.

                jsulmJ 1 Reply Last reply
                0
                • O onurcevik

                  @jsulm I edited and added main.cpp to my question.

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

                  @onurcevik A loop with sleep() inside (before starting event loop) and you expect this to work? It will not. See what @SGaist wrote.

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

                  O 1 Reply Last reply
                  2
                  • jsulmJ jsulm

                    @onurcevik A loop with sleep() inside (before starting event loop) and you expect this to work? It will not. See what @SGaist wrote.

                    O Offline
                    O Offline
                    onurcevik
                    wrote on last edited by
                    #9

                    @jsulm @SGaist Sorry for my noob mistakes. I searched event loop in QT but I couldn't find how to implement it. Can you help me with that ? Should I use Threads ?

                    jsulmJ 1 Reply Last reply
                    0
                    • O onurcevik

                      @jsulm @SGaist Sorry for my noob mistakes. I searched event loop in QT but I couldn't find how to implement it. Can you help me with that ? Should I use Threads ?

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

                      @onurcevik

                      return a.exec();
                      

                      this starts the event loop. No need for threads. Use asynchronous nature of QUdpSocket instead. You should take a look at one of the example applications for networking.

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

                      O 1 Reply Last reply
                      0
                      • jsulmJ jsulm

                        @onurcevik

                        return a.exec();
                        

                        this starts the event loop. No need for threads. Use asynchronous nature of QUdpSocket instead. You should take a look at one of the example applications for networking.

                        O Offline
                        O Offline
                        onurcevik
                        wrote on last edited by onurcevik
                        #11

                        @jsulm I deleted sleep and for loop it still did not work. I did take a look couple of examples even tried to run it. A pinpoint to what wrongs with my code would be really helpful. Since removing sleep and for did not work either.

                        Edit: I will switch to QWidget application and try to send through a button click.

                        aha_1980A 1 Reply Last reply
                        0
                        • O onurcevik

                          @jsulm I deleted sleep and for loop it still did not work. I did take a look couple of examples even tried to run it. A pinpoint to what wrongs with my code would be really helpful. Since removing sleep and for did not work either.

                          Edit: I will switch to QWidget application and try to send through a button click.

                          aha_1980A Offline
                          aha_1980A Offline
                          aha_1980
                          Lifetime Qt Champion
                          wrote on last edited by
                          #12

                          Hi @onurcevik,

                          • On which platform are you?
                          • Which Qt version do you use?
                          • Do you have multiple network adapters in your computer?

                          Regards

                          Qt has to stay free or it will die.

                          O 1 Reply Last reply
                          0
                          • aha_1980A aha_1980

                            Hi @onurcevik,

                            • On which platform are you?
                            • Which Qt version do you use?
                            • Do you have multiple network adapters in your computer?

                            Regards

                            O Offline
                            O Offline
                            onurcevik
                            wrote on last edited by
                            #13

                            @aha_1980

                            • I am on Kubuntu
                            • I use Qt Creator 4.6.2 Based on Qt 5.11.1 (GCC 8.1.0, 64 bit)
                            • I only have 1 modem.
                            aha_1980A 1 Reply Last reply
                            0
                            • O onurcevik

                              @aha_1980

                              • I am on Kubuntu
                              • I use Qt Creator 4.6.2 Based on Qt 5.11.1 (GCC 8.1.0, 64 bit)
                              • I only have 1 modem.
                              aha_1980A Offline
                              aha_1980A Offline
                              aha_1980
                              Lifetime Qt Champion
                              wrote on last edited by aha_1980
                              #14

                              @onurcevik

                              Can you please post the output of ifconfig -a?

                              It is likely that your packet is not even send out. You can check that with Wireshark.

                              Edit:

                              I use Qt Creator 4.6.2 Based on Qt 5.11.1 (GCC 8.1.0, 64 bit)

                              That is the Creator version, not the Qt version I asked for.

                              Qt has to stay free or it will die.

                              O 1 Reply Last reply
                              0
                              • aha_1980A aha_1980

                                @onurcevik

                                Can you please post the output of ifconfig -a?

                                It is likely that your packet is not even send out. You can check that with Wireshark.

                                Edit:

                                I use Qt Creator 4.6.2 Based on Qt 5.11.1 (GCC 8.1.0, 64 bit)

                                That is the Creator version, not the Qt version I asked for.

                                O Offline
                                O Offline
                                onurcevik
                                wrote on last edited by
                                #15

                                @aha_1980

                                That is the Creator version, not the Qt version I asked for.

                                I use Qt Creator 4.6.2 Based on Qt 5.11.1 (GCC 8.1.0, 64 bit)

                                QT version is next to the QT Creator Version which is 5.11.1

                                the output of ifconfig -a :

                                enp27s0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
                                        inet 192.168.2.44  netmask 255.255.255.0  broadcast 192.168.2.255
                                        inet6 fe80::a98d:26da:2afd:3299  prefixlen 64  scopeid 0x20<link>
                                        ether 00:d8:61:15:7d:b8  txqueuelen 1000  (Ethernet)
                                        RX packets 37659  bytes 41459688 (41.4 MB)
                                        RX errors 0  dropped 0  overruns 0  frame 0
                                        TX packets 27301  bytes 2684010 (2.6 MB)
                                        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
                                
                                lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
                                        inet 127.0.0.1  netmask 255.0.0.0
                                        inet6 ::1  prefixlen 128  scopeid 0x10<host>
                                        loop  txqueuelen 1000  (Local Loopback)
                                        RX packets 1963  bytes 180145 (180.1 KB)
                                        RX errors 0  dropped 0  overruns 0  frame 0
                                        TX packets 1963  bytes 180145 (180.1 KB)
                                        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
                                
                                

                                @aha_1980 said in QUdpSocket does not trigger ReadyRead signal:

                                aha_1980A 1 Reply Last reply
                                0
                                • O onurcevik

                                  @aha_1980

                                  That is the Creator version, not the Qt version I asked for.

                                  I use Qt Creator 4.6.2 Based on Qt 5.11.1 (GCC 8.1.0, 64 bit)

                                  QT version is next to the QT Creator Version which is 5.11.1

                                  the output of ifconfig -a :

                                  enp27s0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
                                          inet 192.168.2.44  netmask 255.255.255.0  broadcast 192.168.2.255
                                          inet6 fe80::a98d:26da:2afd:3299  prefixlen 64  scopeid 0x20<link>
                                          ether 00:d8:61:15:7d:b8  txqueuelen 1000  (Ethernet)
                                          RX packets 37659  bytes 41459688 (41.4 MB)
                                          RX errors 0  dropped 0  overruns 0  frame 0
                                          TX packets 27301  bytes 2684010 (2.6 MB)
                                          TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
                                  
                                  lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
                                          inet 127.0.0.1  netmask 255.0.0.0
                                          inet6 ::1  prefixlen 128  scopeid 0x10<host>
                                          loop  txqueuelen 1000  (Local Loopback)
                                          RX packets 1963  bytes 180145 (180.1 KB)
                                          RX errors 0  dropped 0  overruns 0  frame 0
                                          TX packets 1963  bytes 180145 (180.1 KB)
                                          TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
                                  
                                  

                                  @aha_1980 said in QUdpSocket does not trigger ReadyRead signal:

                                  aha_1980A Offline
                                  aha_1980A Offline
                                  aha_1980
                                  Lifetime Qt Champion
                                  wrote on last edited by
                                  #16

                                  @onurcevik said in QUdpSocket does not trigger ReadyRead signal:

                                  QT version is next to the QT Creator Version which is 5.11.1

                                  That is the Qt version Creator is built with, which is not necessarily the version you are using to compile your program.

                                  From you ifconfig I see, that there is only one physical interface., so bind should select the correct one.

                                  Please check with wireshark that the packet is sent out.

                                  Qt has to stay free or it will die.

                                  O 1 Reply Last reply
                                  1
                                  • SGaistS Offline
                                    SGaistS Offline
                                    SGaist
                                    Lifetime Qt Champion
                                    wrote on last edited by
                                    #17

                                    Just for the sake of simplification, did you take a look at the examples linked in the QUdpSocket documentation ?

                                    Thinking about:

                                    Broadcast Sender
                                    Broadcast Receiver
                                    Multicast Sender
                                    Multicast Receiver

                                    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
                                    4
                                    • aha_1980A aha_1980

                                      @onurcevik said in QUdpSocket does not trigger ReadyRead signal:

                                      QT version is next to the QT Creator Version which is 5.11.1

                                      That is the Qt version Creator is built with, which is not necessarily the version you are using to compile your program.

                                      From you ifconfig I see, that there is only one physical interface., so bind should select the correct one.

                                      Please check with wireshark that the packet is sent out.

                                      O Offline
                                      O Offline
                                      onurcevik
                                      wrote on last edited by onurcevik
                                      #18

                                      @aha_1980 I now realized that I can't even send the socket due to large size which is 1 mb. Do you know how can I divide it into chunks of bytes ? Or should I create new topic?

                                      1 Reply Last reply
                                      0
                                      • SGaistS Offline
                                        SGaistS Offline
                                        SGaist
                                        Lifetime Qt Champion
                                        wrote on last edited by
                                        #19

                                        What exactly is the goal of your application ?

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

                                        O 1 Reply Last reply
                                        1
                                        • SGaistS SGaist

                                          What exactly is the goal of your application ?

                                          O Offline
                                          O Offline
                                          onurcevik
                                          wrote on last edited by
                                          #20

                                          @SGaist I want to take pictures from camera then send and recieve them continuously in order to implement a real time video streaming application. The reason I am sending images instead of video is because I will later work on image processing. I realized that image size is too big for this which is 6404803 BYTES. But I also can't switch to TCP because I need the speed of UDP.

                                          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