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 receive Datagram into QtConcurrent
Forum Updated to NodeBB v4.3 + New Features

QUdpSocket receive Datagram into QtConcurrent

Scheduled Pinned Locked Moved Solved General and Desktop
24 Posts 4 Posters 3.2k 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.
  • JonBJ JonB

    @LudoFR
    Well, firstly looks like VS & Qt are similar, which is good news. Secondly surprised it seems receiver is receiving more than sent? I don't think each side agrees on exactly what 1 second is :D

    LudoFRL Offline
    LudoFRL Offline
    LudoFR
    wrote on last edited by LudoFR
    #21

    @JonB said in QUdpSocket receive Datagram into QtConcurrent:

    Secondly surprised it seems receiver is receiving more than sent?

    @JonB Thanks god I'm not the only one...

    @JonB said in QUdpSocket receive Datagram into QtConcurrent:

    I don't think each side agrees on exactly what 1 second is :D

    Science....is not an exact science... did performance computer impact that kind of information? Sender is an Asus Rog Strix3 with a config lower than my PC

    JonBJ 1 Reply Last reply
    0
    • LudoFRL LudoFR

      Hello everyone,

      Still learning on Qt, and I try to run a QUdpSocket receiving UDP data into a QtConcurrent thread, for avoiding GUI freezing.

      Here my main.cpp

      If i call "testComThread()" my function work and I receive my data, but if i call "QtConcurrent::run(testComThread);" I dont see any data into my console and my breakpoint in my "while" is never reached.

      Did I need to call a kind of dispatcher or something maybe? This is my first use of QtConcurrent. Thanks in advance.

      #include <QGuiApplication>
      #include <QQmlApplicationEngine>
      #include <Manage/variablesglobales.h>
      #include <Manage/communications.h>
      #include <QtConcurrent>
      
      void testComThread();
      
      int main(int argc, char *argv[])
      {
      #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
          QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
      #endif
      
          QGuiApplication app(argc, argv);
      
          QQmlApplicationEngine engine;
          const QUrl url(QStringLiteral("qrc:/main.qml"));
          QObject::connect(&engine, &QQmlApplicationEngine::objectCreated,
                           &app, [url](QObject *obj, const QUrl &objUrl) {
              if (!obj && url == objUrl)
                  QCoreApplication::exit(-1);
          }, Qt::QueuedConnection);
          engine.load(url);
      
      
          QFuture<void> test1 = QtConcurrent::run(testComThread);
          //testComThread();
      
      
          return app.exec();
      }
      
      void testComThread()
      {
          Communications* com = new Communications();
          com->InitialiseConnection();
      }
      

      my communication.cpp

      #include "communications.h"
      
      Communications::Communications(QObject *parent) : QObject(parent)
      {
      
      }
      
      void Communications::InitialiseConnection()
      {
          udpSock = new QUdpSocket(this);
          udpSock->bind(QHostAddress::Any,50003);
      
         connect(udpSock,&QUdpSocket::readyRead,this,&Communications::ReceiveUdpPackage);
      }
      
      void Communications::ReceiveUdpPackage()
      {
          QHostAddress sender;
          quint16  port;
      
          while (udpSock->hasPendingDatagrams())
          {
              QByteArray datagram;
              datagram.resize(udpSock->pendingDatagramSize());
              udpSock->readDatagram(datagram.data(),datagram.size(),&sender,&port);
              qDebug() <<"Message From :" << datagram.data();
          }
      }
      

      Thank you in advance.

      jeremy_kJ Offline
      jeremy_kJ Offline
      jeremy_k
      wrote on last edited by jeremy_k
      #22

      @LudoFR said in QUdpSocket receive Datagram into QtConcurrent:

      Hello everyone,

      Still learning on Qt, and I try to run a QUdpSocket receiving UDP data into a QtConcurrent thread, for avoiding GUI freezing.

      Here my main.cpp

      If i call "testComThread()" my function work and I receive my data, but if i call "QtConcurrent::run(testComThread);" I dont see any data into my console and my breakpoint in my "while" is never reached.

      Did I need to call a kind of dispatcher or something maybe? This is my first use of QtConcurrent. Thanks in advance.

      Truncated code:

      int main(int argc, char *argv[])
      {
          QFuture<void> test1 = QtConcurrent::run(testComThread);
          return app.exec();
      }
      
      void testComThread()
      {
          Communications* com = new Communications();
          com->InitialiseConnection();
      
          udpSock = new QUdpSocket(com);
          udpSock->bind(QHostAddress::Any,50003);
      
          connect(udpSock,&QUdpSocket::readyRead,this,&Communications::ReceiveUdpPackage);
      }
      

      This won't work. QtConcurrent::run() runs a function in a thread that doesn't have an event loop, and may cease to exist when the invoked function returns.

      Regarding QML: The language has fairly strong rules regarding capitalization. Types should begin with an upper case letter. Properties and functions should begin with a lower case letter. Following the convention in C++ will make integration easier.

      Asking a question about code? http://eel.is/iso-c++/testcase/

      LudoFRL 1 Reply Last reply
      0
      • LudoFRL LudoFR

        @JonB said in QUdpSocket receive Datagram into QtConcurrent:

        Secondly surprised it seems receiver is receiving more than sent?

        @JonB Thanks god I'm not the only one...

        @JonB said in QUdpSocket receive Datagram into QtConcurrent:

        I don't think each side agrees on exactly what 1 second is :D

        Science....is not an exact science... did performance computer impact that kind of information? Sender is an Asus Rog Strix3 with a config lower than my PC

        JonBJ Online
        JonBJ Online
        JonB
        wrote on last edited by
        #23

        @LudoFR
        Don't worry about exact timings. Your 10 second timing indicates there are around 13,000 datagrams per second, which is just what you were originally expecting. Just make sure whatever processing you do on those datagrams is fast!

        1 Reply Last reply
        0
        • jeremy_kJ jeremy_k

          @LudoFR said in QUdpSocket receive Datagram into QtConcurrent:

          Hello everyone,

          Still learning on Qt, and I try to run a QUdpSocket receiving UDP data into a QtConcurrent thread, for avoiding GUI freezing.

          Here my main.cpp

          If i call "testComThread()" my function work and I receive my data, but if i call "QtConcurrent::run(testComThread);" I dont see any data into my console and my breakpoint in my "while" is never reached.

          Did I need to call a kind of dispatcher or something maybe? This is my first use of QtConcurrent. Thanks in advance.

          Truncated code:

          int main(int argc, char *argv[])
          {
              QFuture<void> test1 = QtConcurrent::run(testComThread);
              return app.exec();
          }
          
          void testComThread()
          {
              Communications* com = new Communications();
              com->InitialiseConnection();
          
              udpSock = new QUdpSocket(com);
              udpSock->bind(QHostAddress::Any,50003);
          
              connect(udpSock,&QUdpSocket::readyRead,this,&Communications::ReceiveUdpPackage);
          }
          

          This won't work. QtConcurrent::run() runs a function in a thread that doesn't have an event loop, and may cease to exist when the invoked function returns.

          Regarding QML: The language has fairly strong rules regarding capitalization. Types should begin with an upper case letter. Properties and functions should begin with a lower case letter. Following the convention in C++ will make integration easier.

          LudoFRL Offline
          LudoFRL Offline
          LudoFR
          wrote on last edited by
          #24

          @jeremy_k said in QUdpSocket receive Datagram into QtConcurrent:

          Regarding QML: The language has fairly strong rules regarding capitalization. Types should begin with an upper case letter. Properties and functions should begin with a lower case letter. Following the convention in C++ will make integration easier.

          Agreed and thanks, but i need to make a choice for my compagny and our futur tools, C++, python, Qt, PyCharm, VS.. first I check the language, the possibilities, time to develop with, the compatibily with all the dependency we need...

          After our choice, I will check about the code convention, and use it in the final project, hope you have remarked this is not a real project !

          But it's clearly not my priority now....

          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