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 and readyRead signal
Qt 6.11 is out! See what's new in the release blog

QUdpSocket and readyRead signal

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 3 Posters 1.6k 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.
  • I Offline
    I Offline
    iaguirre
    wrote on last edited by VRonin
    #1

    I have implemented a simple UDP server according the specifcations of QUdpSocket class (Qt 5.10.1).

    But on my code, the readySignal never received. I try to send data to the udp server via nc util: echo "Hello" | nc -u localhost 1234

    This is my code:

    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
        MainWindow w;
        w.show();
    
        MyServer server;
        server.initSocket();
        return a.exec();
    }
    
    and MyServer class implementation:
    
    MyServer::MyServer(QObject *parent) : QObject(parent)
    {
    }
    
    void MyServer::initSocket()
    {
        udpSocket = new QUdpSocket(this);
        if(!udpSocket->bind(QHostAddress::Any, 1234, QAbstractSocket::BindFlag::ShareAddress))
          {
              qDebug() << "bind error";
          }
    
          if(udpSocket->state() == QUdpSocket::BoundState)
              qDebug() << "GREAT!!!";
    
        connect(udpSocket, SIGNAL(readyRead()),
                this, SLOT(readPendingDatagrams()));
    }
    
    void MyServer::readPendingDatagrams()
    {
        QByteArray Buffer;
        int bufSize = udpSocket->pendingDatagramSize();
        Buffer.resize(bufSize);
    
        QHostAddress sender;
        quint16 senderPort;
        udpSocket->readDatagram(Buffer.data(),Buffer.size(),&sender,&senderPort);
    
    }
    

    Any help would be appreciated.
    Thanks,
    IAE

    [Edit aha_1980: Fixed typo in title]

    Pablo J. RoginaP 1 Reply Last reply
    0
    • I iaguirre

      I have implemented a simple UDP server according the specifcations of QUdpSocket class (Qt 5.10.1).

      But on my code, the readySignal never received. I try to send data to the udp server via nc util: echo "Hello" | nc -u localhost 1234

      This is my code:

      int main(int argc, char *argv[])
      {
          QApplication a(argc, argv);
          MainWindow w;
          w.show();
      
          MyServer server;
          server.initSocket();
          return a.exec();
      }
      
      and MyServer class implementation:
      
      MyServer::MyServer(QObject *parent) : QObject(parent)
      {
      }
      
      void MyServer::initSocket()
      {
          udpSocket = new QUdpSocket(this);
          if(!udpSocket->bind(QHostAddress::Any, 1234, QAbstractSocket::BindFlag::ShareAddress))
            {
                qDebug() << "bind error";
            }
      
            if(udpSocket->state() == QUdpSocket::BoundState)
                qDebug() << "GREAT!!!";
      
          connect(udpSocket, SIGNAL(readyRead()),
                  this, SLOT(readPendingDatagrams()));
      }
      
      void MyServer::readPendingDatagrams()
      {
          QByteArray Buffer;
          int bufSize = udpSocket->pendingDatagramSize();
          Buffer.resize(bufSize);
      
          QHostAddress sender;
          quint16 senderPort;
          udpSocket->readDatagram(Buffer.data(),Buffer.size(),&sender,&senderPort);
      
      }
      

      Any help would be appreciated.
      Thanks,
      IAE

      [Edit aha_1980: Fixed typo in title]

      Pablo J. RoginaP Offline
      Pablo J. RoginaP Offline
      Pablo J. Rogina
      wrote on last edited by
      #2

      @iaguirre are you able to check your server is really listening on port 1234?

      $ netstat | grep 1234
      

      Upvote the answer(s) that helped you solve the issue
      Use "Topic Tools" button to mark your post as Solved
      Add screenshots via postimage.org
      Don't ask support requests via chat/PM. Please use the forum so others can benefit from the solution in the future

      1 Reply Last reply
      1
      • I Offline
        I Offline
        iaguirre
        wrote on last edited by VRonin
        #3

        I have finally resolved the problem by makig the connect before the bind process. Now, it works!!

        void MyServer::initSocket()
        {
            udpSocket = new QUdpSocket(this);
            connect(udpSocket, SIGNAL(readyRead()),
                    this, SLOT(readPendingDatagrams()));
        
            if(!udpSocket->bind(QHostAddress::Any, 1234, QAbstractSocket::BindFlag::ShareAddress))
              {
                  qDebug() << "bind error";
              }
        
              if(udpSocket->state() == QUdpSocket::BoundState)
                  qDebug() << "GREAT!!!";
        
        }
        
        1 Reply Last reply
        1
        • KroMignonK Offline
          KroMignonK Offline
          KroMignon
          wrote on last edited by
          #4

          Thank's a lot for sharing your workaround.. I've got same issue after updating my application from Qt 5.4.2 to 5.11.2!

          Performing connect to QUdpSocket::readyRead() signal before doing QUdpSocket::bind() also resolve it for me!

          Regards

          It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

          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