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 paradigma looks confusing

QUDPsocket paradigma looks confusing

Scheduled Pinned Locked Moved Solved General and Desktop
udpdatagram
4 Posts 3 Posters 743 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
    Tamis2018
    wrote on 15 Aug 2018, 16:45 last edited by
    #1

    This code taken from the net looks easy to understand. But I doubt that!

    connect(&udpSocket, SIGNAL(readyRead()),
    this, SLOT(processPendingDatagrams()))

    void WeatherStation::processPendingDatagrams()
    {
    QByteArray datagram;

    do {
    datagram.resize(udpSocket.pendingDatagramSize());
    udpSocket.readDatagram(datagram.data(), datagram.size());
    } while (udpSocket.hasPendingDatagrams());

    etc

    }

    Basically while (udpSocket.hasPendingDatagrams()) looks a bit odd. Let say that I have two or three datagrams. How many times connect will signal that
    has datagram to be processed? ProcessPending digram will process one or all datagrams arrived? Or in this case I might read and read second datagram on the top of the first one?
    That might not be what I wanted?

    J P 2 Replies Last reply 15 Aug 2018, 17:12
    0
    • T Tamis2018
      15 Aug 2018, 16:45

      This code taken from the net looks easy to understand. But I doubt that!

      connect(&udpSocket, SIGNAL(readyRead()),
      this, SLOT(processPendingDatagrams()))

      void WeatherStation::processPendingDatagrams()
      {
      QByteArray datagram;

      do {
      datagram.resize(udpSocket.pendingDatagramSize());
      udpSocket.readDatagram(datagram.data(), datagram.size());
      } while (udpSocket.hasPendingDatagrams());

      etc

      }

      Basically while (udpSocket.hasPendingDatagrams()) looks a bit odd. Let say that I have two or three datagrams. How many times connect will signal that
      has datagram to be processed? ProcessPending digram will process one or all datagrams arrived? Or in this case I might read and read second datagram on the top of the first one?
      That might not be what I wanted?

      J Offline
      J Offline
      JonB
      wrote on 15 Aug 2018, 17:12 last edited by JonB
      #2

      @Tamis2018
      Not sure what you mean/what the problem is! :)

      This code taken from the net

      Then why not tell us the link?? Or do you enjoy setting us puzzles? :)

      Basically while (udpSocket.hasPendingDatagrams()) looks a bit odd.

      Why?!

      Let say that I have two or three datagrams. How many times connect will signal that
      has datagram to be processed?

      The signal is sent on readyRead().

      ProcessPending digram will process one or all datagrams arrived?

      processPendingDatagrams() has a loop. It will process as many datagrams as it finds are available, then it will exit.

      Or in this case I might read and read second datagram on the top of the first one?
      That might not be what I wanted?

      The code is just showing you how to read each datagram into a QByteArray. Yes, each one will overwrite in that buffer. I suspect the "code taken from the net" you mention intended you to do your processing of that data in the loop, after you have read one datagram into the buffer before you go read the next one on top of it!

      T 1 Reply Last reply 15 Aug 2018, 21:32
      2
      • T Tamis2018
        15 Aug 2018, 16:45

        This code taken from the net looks easy to understand. But I doubt that!

        connect(&udpSocket, SIGNAL(readyRead()),
        this, SLOT(processPendingDatagrams()))

        void WeatherStation::processPendingDatagrams()
        {
        QByteArray datagram;

        do {
        datagram.resize(udpSocket.pendingDatagramSize());
        udpSocket.readDatagram(datagram.data(), datagram.size());
        } while (udpSocket.hasPendingDatagrams());

        etc

        }

        Basically while (udpSocket.hasPendingDatagrams()) looks a bit odd. Let say that I have two or three datagrams. How many times connect will signal that
        has datagram to be processed? ProcessPending digram will process one or all datagrams arrived? Or in this case I might read and read second datagram on the top of the first one?
        That might not be what I wanted?

        P Offline
        P Offline
        Pablo J. Rogina
        wrote on 15 Aug 2018, 17:57 last edited by
        #3

        @Tamis2018 have you tried running the whole "code taken from the net"? Does it work?

        You may want to check QUdpSocket documentation, which shows a quite similar way to read arriving datagrams.

        Essentially, you act on readyRead() signal "emitted whenever datagrams arrive" and loop until there are no more datagrams to read...

        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
        2
        • J JonB
          15 Aug 2018, 17:12

          @Tamis2018
          Not sure what you mean/what the problem is! :)

          This code taken from the net

          Then why not tell us the link?? Or do you enjoy setting us puzzles? :)

          Basically while (udpSocket.hasPendingDatagrams()) looks a bit odd.

          Why?!

          Let say that I have two or three datagrams. How many times connect will signal that
          has datagram to be processed?

          The signal is sent on readyRead().

          ProcessPending digram will process one or all datagrams arrived?

          processPendingDatagrams() has a loop. It will process as many datagrams as it finds are available, then it will exit.

          Or in this case I might read and read second datagram on the top of the first one?
          That might not be what I wanted?

          The code is just showing you how to read each datagram into a QByteArray. Yes, each one will overwrite in that buffer. I suspect the "code taken from the net" you mention intended you to do your processing of that data in the loop, after you have read one datagram into the buffer before you go read the next one on top of it!

          T Offline
          T Offline
          Tamis2018
          wrote on 15 Aug 2018, 21:32 last edited by
          #4

          @JonB you might be right.
          http://www.informit.com/articles/article.aspx?p=1405552&seqNum=4
          in fact I could see that if there is pending datagram, read it, and then again, and as well process after each if statement. Ok. I understand it now.

          1 Reply Last reply
          0

          2/4

          15 Aug 2018, 17:12

          • Login

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