Important: Please read the Qt Code of Conduct -

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

  • 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()

        {  QByteArray datagram;
           udpSocket->readDatagram(,   datagram.size());
           qDebug<<"UDP received";


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

    [edit: added missing coding tags @ SGaist]

  • 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":

    I hope i have helped you.

  • I found the site below and follow what they are doing.

    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

Log in to reply