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. ReadyRead() signal in QUdpSocket isn't being triggered.

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

Scheduled Pinned Locked Moved General and Desktop
3 Posts 2 Posters 3.4k 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.
  • M Offline
    M Offline
    matthazley
    wrote on last edited by
    #1

    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
    }

    }
    @

    1 Reply Last reply
    0
    • T Offline
      T Offline
      tomma
      wrote on last edited by
      #2

      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.

      1 Reply Last reply
      0
      • M Offline
        M Offline
        matthazley
        wrote on last edited by
        #3

        Thanks - that was a silly mistake.

        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