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. why is data not transmitted over udp?

why is data not transmitted over udp?

Scheduled Pinned Locked Moved Solved General and Desktop
qt5.15.2udp
5 Posts 2 Posters 409 Views
  • 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.
  • T Offline
    T Offline
    timob256
    wrote on 27 Jul 2023, 16:53 last edited by
    #1

    I'm sending a message by UDP (-9.58924 2) and this is what I get ( "" ) :

    0   1
    str_priem:  ""
    -9.58924   2
    str_priem:  ""
    -5.44021   3
    str_priem:  ""
    6.50288   4
    str_priem:  ""
    9.12945   5
    str_priem:  ""
    -1.32352   6
    str_priem:  ""
    -9.88032   7
    str_priem:  ""
    

    here is the code :

    one sends the other receives

    // тут отправляем сообщение
    void MainWindow::onTimeoutStart()
    {
    
        str_ip = ui->lE_enover_id->text();
        str_port = ui->lE_enover_port->text();
        int_port = str_port.toInt(); // превращаем в инты
    
         // сообщение
        // состоит из двух чисел первое Х второе У
        if(grad >=360 )
        {
            grad = 0;
        }
        data_x = 10 * sin(grad);
        grad = grad + 5;
        if(data_y >= 100)
            data_y = 0;
        data_y = data_y + 1;
    
        //отправка
        QByteArray baDatagram_out;
        QDataStream out(&baDatagram_out, QIODevice::WriteOnly);
        out.setVersion(QDataStream::Qt_5_3);
        qDebug() << data_x << " " << data_y;
        out << data_x;
        out << data_y;
        if(ui->lE_enover_id->text().isEmpty() == true)
        {
            m_pudp->writeDatagram(baDatagram_out, QHostAddress::LocalHost, int_port);
        }
        else
        {
            m_pudp->writeDatagram(baDatagram_out, QHostAddress(str_ip),  int_port);
        }
    }
    

    receive :

    void MainWindow::slotProcessDatagrams()
    {
        QByteArray baDatagram_in;
        do {
            baDatagram_in.resize(m_pudp_in->pendingDatagramSize());
            m_pudp_in->readDatagram(baDatagram_in.data(), baDatagram_in.size());
        } while(m_pudp_in->hasPendingDatagrams());
    
        QDataStream in(&baDatagram_in, QIODevice::ReadOnly);
        in.setVersion(QDataStream::Qt_5_3);
        QString str_priem;
        in >>  str_priem;
    
        qDebug() << "str_priem: " << str_priem;
        str_priem.clear();
    }
    

    why does only an empty message arrive without "filling"??

    введите сюда описание изображения

    C T 2 Replies Last reply 27 Jul 2023, 17:05
    0
    • T timob256
      28 Jul 2023, 14:36

      @Christian-Ehrlicher said in why is data not transmitted over udp?:

      you write two doubles but read a QString - how should this work?

      Thank you for the offer. But alas, I do not know what to write to make it work.

      I am not a mathematician and have no education, please tell me what to write.
      The 3rd grade of junior high school is all I've finished ;_; .

      C Online
      C Online
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on 28 Jul 2023, 14:39 last edited by
      #4

      @timob256 said in why is data not transmitted over udp?:

      Thank you for the offer. But alas, I do not know what to write to make it work.

      When you put two numbers into a QDataStream you should also read two numbers and not a string - this has nothing to do with programming - when you put two apples into a bag you can't get an banana out of it...

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      1 Reply Last reply
      2
      • T timob256
        27 Jul 2023, 16:53

        I'm sending a message by UDP (-9.58924 2) and this is what I get ( "" ) :

        0   1
        str_priem:  ""
        -9.58924   2
        str_priem:  ""
        -5.44021   3
        str_priem:  ""
        6.50288   4
        str_priem:  ""
        9.12945   5
        str_priem:  ""
        -1.32352   6
        str_priem:  ""
        -9.88032   7
        str_priem:  ""
        

        here is the code :

        one sends the other receives

        // тут отправляем сообщение
        void MainWindow::onTimeoutStart()
        {
        
            str_ip = ui->lE_enover_id->text();
            str_port = ui->lE_enover_port->text();
            int_port = str_port.toInt(); // превращаем в инты
        
             // сообщение
            // состоит из двух чисел первое Х второе У
            if(grad >=360 )
            {
                grad = 0;
            }
            data_x = 10 * sin(grad);
            grad = grad + 5;
            if(data_y >= 100)
                data_y = 0;
            data_y = data_y + 1;
        
            //отправка
            QByteArray baDatagram_out;
            QDataStream out(&baDatagram_out, QIODevice::WriteOnly);
            out.setVersion(QDataStream::Qt_5_3);
            qDebug() << data_x << " " << data_y;
            out << data_x;
            out << data_y;
            if(ui->lE_enover_id->text().isEmpty() == true)
            {
                m_pudp->writeDatagram(baDatagram_out, QHostAddress::LocalHost, int_port);
            }
            else
            {
                m_pudp->writeDatagram(baDatagram_out, QHostAddress(str_ip),  int_port);
            }
        }
        

        receive :

        void MainWindow::slotProcessDatagrams()
        {
            QByteArray baDatagram_in;
            do {
                baDatagram_in.resize(m_pudp_in->pendingDatagramSize());
                m_pudp_in->readDatagram(baDatagram_in.data(), baDatagram_in.size());
            } while(m_pudp_in->hasPendingDatagrams());
        
            QDataStream in(&baDatagram_in, QIODevice::ReadOnly);
            in.setVersion(QDataStream::Qt_5_3);
            QString str_priem;
            in >>  str_priem;
        
            qDebug() << "str_priem: " << str_priem;
            str_priem.clear();
        }
        

        why does only an empty message arrive without "filling"??

        введите сюда описание изображения

        C Online
        C Online
        Christian Ehrlicher
        Lifetime Qt Champion
        wrote on 27 Jul 2023, 17:05 last edited by
        #2

        @timob256 you write two doubles but read a QString - how should this work?

        Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
        Visit the Qt Academy at https://academy.qt.io/catalog

        T 1 Reply Last reply 28 Jul 2023, 14:36
        2
        • C Christian Ehrlicher
          27 Jul 2023, 17:05

          @timob256 you write two doubles but read a QString - how should this work?

          T Offline
          T Offline
          timob256
          wrote on 28 Jul 2023, 14:36 last edited by
          #3

          @Christian-Ehrlicher said in why is data not transmitted over udp?:

          you write two doubles but read a QString - how should this work?

          Thank you for the offer. But alas, I do not know what to write to make it work.

          I am not a mathematician and have no education, please tell me what to write.
          The 3rd grade of junior high school is all I've finished ;_; .

          C 1 Reply Last reply 28 Jul 2023, 14:39
          0
          • T timob256
            28 Jul 2023, 14:36

            @Christian-Ehrlicher said in why is data not transmitted over udp?:

            you write two doubles but read a QString - how should this work?

            Thank you for the offer. But alas, I do not know what to write to make it work.

            I am not a mathematician and have no education, please tell me what to write.
            The 3rd grade of junior high school is all I've finished ;_; .

            C Online
            C Online
            Christian Ehrlicher
            Lifetime Qt Champion
            wrote on 28 Jul 2023, 14:39 last edited by
            #4

            @timob256 said in why is data not transmitted over udp?:

            Thank you for the offer. But alas, I do not know what to write to make it work.

            When you put two numbers into a QDataStream you should also read two numbers and not a string - this has nothing to do with programming - when you put two apples into a bag you can't get an banana out of it...

            Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
            Visit the Qt Academy at https://academy.qt.io/catalog

            1 Reply Last reply
            2
            • T timob256
              27 Jul 2023, 16:53

              I'm sending a message by UDP (-9.58924 2) and this is what I get ( "" ) :

              0   1
              str_priem:  ""
              -9.58924   2
              str_priem:  ""
              -5.44021   3
              str_priem:  ""
              6.50288   4
              str_priem:  ""
              9.12945   5
              str_priem:  ""
              -1.32352   6
              str_priem:  ""
              -9.88032   7
              str_priem:  ""
              

              here is the code :

              one sends the other receives

              // тут отправляем сообщение
              void MainWindow::onTimeoutStart()
              {
              
                  str_ip = ui->lE_enover_id->text();
                  str_port = ui->lE_enover_port->text();
                  int_port = str_port.toInt(); // превращаем в инты
              
                   // сообщение
                  // состоит из двух чисел первое Х второе У
                  if(grad >=360 )
                  {
                      grad = 0;
                  }
                  data_x = 10 * sin(grad);
                  grad = grad + 5;
                  if(data_y >= 100)
                      data_y = 0;
                  data_y = data_y + 1;
              
                  //отправка
                  QByteArray baDatagram_out;
                  QDataStream out(&baDatagram_out, QIODevice::WriteOnly);
                  out.setVersion(QDataStream::Qt_5_3);
                  qDebug() << data_x << " " << data_y;
                  out << data_x;
                  out << data_y;
                  if(ui->lE_enover_id->text().isEmpty() == true)
                  {
                      m_pudp->writeDatagram(baDatagram_out, QHostAddress::LocalHost, int_port);
                  }
                  else
                  {
                      m_pudp->writeDatagram(baDatagram_out, QHostAddress(str_ip),  int_port);
                  }
              }
              

              receive :

              void MainWindow::slotProcessDatagrams()
              {
                  QByteArray baDatagram_in;
                  do {
                      baDatagram_in.resize(m_pudp_in->pendingDatagramSize());
                      m_pudp_in->readDatagram(baDatagram_in.data(), baDatagram_in.size());
                  } while(m_pudp_in->hasPendingDatagrams());
              
                  QDataStream in(&baDatagram_in, QIODevice::ReadOnly);
                  in.setVersion(QDataStream::Qt_5_3);
                  QString str_priem;
                  in >>  str_priem;
              
                  qDebug() << "str_priem: " << str_priem;
                  str_priem.clear();
              }
              

              why does only an empty message arrive without "filling"??

              введите сюда описание изображения

              T Offline
              T Offline
              timob256
              wrote on 28 Jul 2023, 15:52 last edited by
              #5

              @timob256 this work

              QString data_str = QString::number(data_x)+ " "+QString::number(data_y);
              
              out << data_str;
              
              qDebug() <<data_str ;
              

              введите сюда описание изображения

              1 Reply Last reply
              0
              • T timob256 has marked this topic as solved on 28 Jul 2023, 15:53

              5/5

              28 Jul 2023, 15:52

              • Login

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