Unable to receive message QUdpSocket



  • i have a problem in line 26
    ERROR :"Unable to receive a message"
    please how to solve this problem in QT 4.8
    ?????????

    @#include "udpserver.h"

    udpserver::udpserver(QObject *parent) :
    QObject(parent)
    {
    listenSocket = new QUdpSocket(this);

    if (!listenSocket->bind(QHostAddress::Any, 8888)) {
       qDebug("Impossible de créer le socket en écoute");
       exit(EXIT_FAILURE);
    }
    
    
    QObject::connect(listenSocket, SIGNAL(readyRead()),
                    this, SLOT(readyRead()));
    

    }

    void udpserver::readyRead()
    {
    while (listenSocket->hasPendingDatagrams() == 0) {

        QByteArray datagram;
    
        datagram.resize(listenSocket->pendingDatagramSize());
    
        if (listenSocket->readDatagram(datagram.data(), datagram.size(), &senderAddress, &senderPort) == -1) {
    
       qDebug() <<"ERROR : "<<listenSocket->errorString();
    
            listenSocket->close();
            exit(EXIT_FAILURE);
        }
    
        msg_reception = datagram.data();
        qDebug() << "From : " << senderAddress.toString() << ':' << senderPort;
        qDebug() << "Message : " << msg_reception;
    
        datagram.clear();
    

    }
    }@



  • i think

    0 = false

    so perhaps try

    @while (listenSocket->hasPendingDatagrams() )
    @ ?



  • if I remove == 0 I do not exceed the while



  • so it never leaves the while loop when a packet is received ? but it throws an error ? which means it does leave the loop?

    i assume that the senderAddress and senderPort are initialised , what happens if you don't specify a senderAddress and senderPort ,they are optional, in ReadDatagram (just for now to see if it helps)



  • i've got some code that does work and it looks like :

    @void MainWindow::processPendingDatagrams()
    {
    while( broadcastReceiverSocket->hasPendingDatagrams())
    {
    QByteArray datagram;
    int theSize = broadcastReceiverSocket->pendingDatagramSize();
    datagram.resize(theSize);
    broadcastReceiverSocket->readDatagram(datagram.data(), datagram.size());

        QString stdg((const QByteArray&) datagram);
         qDebug() << stdg;
      }
    

    }@



  • i have this example an another work and it's work correctly but in this work return ERROR :"Unable to receive a message" !!!!!



  • so could you take the working example and slowly change it to the non-working example and see when it stops working ?

    is it when you start specifing a senderAddress and senderPort in readDatagram() ?



  • it's working here but if i remove the while(1) condition it does not work!!!
    I want one socket happens to me and not more!

    @void udpserver::readyRead()
    {
    while (1) {
    QByteArray datagram;

            if (listenSocket->hasPendingDatagrams()) {
                datagram.resize(listenSocket->pendingDatagramSize());
                if (listenSocket->readDatagram(datagram.data(), datagram.size(),
                                              &senderAddress, &senderPort) == -1) {
                    listenSocket->close();
                    exit(EXIT_FAILURE);
                }
                msg_reception = datagram.data();
    
                qDebug() << "Depuis : " << senderAddress.toString() << ':' << senderPort;
                qDebug() << "Message : " << msg_reception;
    
            }
    
            datagram.clear();
    
            if (msg_reception  == "_EXIT_") {
                listenSocket->close();
                exit(EXIT_SUCCESS);
            }
    
    }
    
    listenSocket->close();
    

    }@



  • i'm not sure i understand what the problem is.

    doesn't readReady() get called automatically by the readyRead() signal each time there is data available ?

    then the while( listenSocket->hasPendingDatagrams() )
    should keep going till there are no more outstanding data packets to be read

    then when new data packets are received by listenSocket the readyRead() signal will fire again and the whole process will start again,



  • does it make any difference if you use

    listenSocket->bind(QHostAddress::Any, 8888, QUdpSocket::ShareAddress | QUdpSocket::ReuseAddressHint)

    ?


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.