ReadyRead() signal in QUdpSocket isn't being triggered.



  • Guys - first time coding for networking stuff so there is possibility I'm doing something silly.

    I am trying to write a small UDP server application.

    I have a non-Qt client transmitting to this applications socket and I have verified this is sending ok using a small UDP echo program (which echoes the data received on a port to the screen) and also, I can see the packets received in wireshark.

    I am using QUdpSocket and it would appear that this Binds ok on setup - but the readyRead() signal doesn't ever seem to get triggered.

    I have included some of my code below - at the minute I am simply trying to emulate the little echo program.

    Just to give some context to the below code - a button press on the UI calls 'setupNewSocket' on a port that is typed in on UI.

    @
    #include "sockethandler.h"

    SocketHandler::SocketHandler(QObject *parent) :
    QObject(parent)
    {
    udpSocket = new QUdpSocket(this);

    connect( &w, SIGNAL(openNewUDPSocket(quint16)), this, SLOT(setupNewSocket(quint16)) );
    connect( this, SIGNAL(printOnUI(QString,QString,QString)), &w, SLOT(updateUI(QString,QString,QString)) );
    
    w.show();
    

    }

    void SocketHandler::readPendingDatagrams()
    {
    while (udpSocket->hasPendingDatagrams())
    {
    QByteArray datagram;
    datagram.resize(udpSocket->pendingDatagramSize());
    QHostAddress sender;
    quint16 senderPort;

         udpSocket->readDatagram(datagram.data(), datagram.size(), &sender, &senderPort);
    
         QString data = QString( datagram.data() );
         QString sender_address = sender.toString();
         QString sender_port = QString("%1").arg(senderPort);
    
         emit printOnUI(data, sender_address, sender_port);
    
    }
    

    }
    void SocketHandler::setupNewSocket(quint16 port)
    {
    if( udpSocket->bind(QHostAddress::LocalHost, port) )
    {
    connect(udpSocket, SIGNAL(readyRead()), this, SLOT(readPendingDatagrams()));
    }
    else
    {
    // bind has failed
    }

    }
    @



  • You bind that socket to localhost: "if( udpSocket->bind(QHostAddress::LocalHost, port) )".
    So your client should be sending data to localhost too.
    You should set it to QHostAddress::Any or networkinterface address to receive from network.



  • Thanks - that was a silly mistake.


Log in to reply
 

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