Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Mobile and Embedded
  4. UDP Socket do not need to run in forever loop(solved)
Forum Updated to NodeBB v4.3 + New Features

UDP Socket do not need to run in forever loop(solved)

Scheduled Pinned Locked Moved Mobile and Embedded
3 Posts 2 Posters 1.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.
  • H Offline
    H Offline
    houmingc
    wrote on last edited by
    #1

    Below is my udp code.
    Do i need to use a thread to read processPendingDatagram() continuously or
    socket constructor will always receive Signal whenever and run processPendingDatagrams()
    thus forever loop not needed.

    @
    void socketudp::processPendingDatagrams()
    {

        while(udpSocket->hasPendingDatagrams();
        {  QByteArray datagram;
           datagram.resize(updSocket->pendingDatagramSize());
           udpSocket->readDatagram(datagram.data(),   datagram.size());
           qDebug<<"UDP received";
    
      }
    

    }

    socketudp::socketudp()
    {
    udpSocket=new QudpSocket(this);
    udpSocket->bind(10010, QUdpSocket::ShareAddress);
    connect(udpsocket, SIGNAL(readyRead()), this, SLOT(processPendingDatagrams()));
    }
    @

    [edit: added missing coding tags @ SGaist]

    1 Reply Last reply
    0
    • L Offline
      L Offline
      leonardo.ramosantos
      wrote on last edited by
      #2

      Hi.

      First of all, why are you using the last connect? That line doesn't do anything.

      Second, you can use a thread and connect a slot in the udpsocket's signal called "readyRead". Everytime udpsocket is receiving data, it calls this signal, so you can read the data incoming.

      Here is a link to a solution like the one i told you about: "link":http://stackoverflow.com/questions/21224894/qt-qudpsocket-readyread-signal-and-corresponding-slot-not-working-as-supposed

      I hope i have helped you.

      1 Reply Last reply
      0
      • H Offline
        H Offline
        houmingc
        wrote on last edited by
        #3

        I found the site below and follow what they are doing.
        http://doc.qt.digia.com/4.6/network-broadcastsender.html

        I found my problem is because i created the socketUDP class object inside
        Mainwindow constructor, application cannot received.
        When i put the object creation outside mainwindow (in main.cpp), data is received successfully. Thanks

        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