Results 1 to 4 of 4

Thread: Not getting readyRead from a QUdpSocket

  1. #1
    Join Date
    Jul 2010
    Posts
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Not getting readyRead from a QUdpSocket

    Hi,
    I've been using QT for about 2 months. Now I have a question.

    I dont get notified with the readyread signal when a udp packet has arrived:

    //----------mainwindow.cpp---------------
    #include "mainwindow.h"
    #include "ui_mainwindow.h"

    MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
    {
    ui->setupUi(this);
    udpSocket = new QUdpSocket(0) ;
    QHostAddress myAddr = QHostAddress("170.126.62.114") ;
    bool result = udpSocket->bind(myAddr, 64009);

    result = connect(udpSocket, SIGNAL(readyRead()),
    this, SLOT(processPendingDatagrams()));
    }

    MainWindow::~MainWindow()
    {
    delete ui;
    }

    void MainWindow:rocessPendingDatagrams()
    {
    QByteArray datagram;

    do {
    datagram.resize(udpSocket->pendingDatagramSize());
    udpSocket->readDatagram(datagram.data(), datagram.size());
    } while (udpSocket->hasPendingDatagrams());

    }

    //----------main.cpp---------------
    int main(int argc, char *argv[])
    {
    QApplication a(argc, argv);
    MainWindow w;
    w.show();
    return a.exec();
    }

    //----------mainwindow.h---------------
    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H
    #include <QMainWindow>
    #include <QUdpSocket>
    namespace Ui {
    class MainWindow;
    }

    class MainWindow : public QMainWindow {
    Q_OBJECT
    public:
    MainWindow(QWidget *parent = 0);
    ~MainWindow();

    protected:
    void changeEvent(QEvent *e);

    private:
    Ui::MainWindow *ui;
    QUdpSocket *udpSocket;
    private slots:
    void processPendingDatagrams();

    };

    #endif // MAINWINDOW_H


    Can someone please tell me what i'm doing wrong?

  2. #2
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Not getting readyRead from a QUdpSocket

    I can see several problems:
    Are you sure 170.126.62.114 is the IP address of your PC? Unless you have more than one interface, it's not really needed either
    You are using new QUdpSocket(0) so the socket has no parent. Use 'this' instead of '0', or free the pointer in your destructor.
    Your processPendingDatagrams() method constantly overwrites it's own data.
    You should be using [ code ] tags.

  3. #3
    Join Date
    Jul 2010
    Posts
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Not getting readyRead from a QUdpSocket

    thanks fjm,

    i am sure that is my address, but i've tried the overloaded bind that just uses the port but that also doesnt work.
    i tried instantiating with 'this' instead of 0 but no go.
    AFA the proc...() method overwriting itself, it doesnt matter. this is just a quickie down and dirty app to isolate my problem in a small space.

    i did try sending UDP dgrams between console apps (non-QT), on my machine, and it works for them. those apps were written using winsock libs.

    Could there be some security issues going on here that QT is querying windows for, but the console apps dont? I'm on a machine at work, and even tho i'm an administrator, i dont have alll privileges.

    BTW: what are [code] tags?

  4. #4
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Not getting readyRead from a QUdpSocket

    Perhaps you can try the Qt broadcast sender/receiver examples. If those work, then you have a problem with your code.

    Also note that UDP packets can be filtered out by some routers are firewalls, so make sure your traffic is on the same subnet for testing purposes.

    [code] tags make your code much more readable on this forum, they preserve spacing and disable emoticons.

    Qt Code:
    1. like this
    2. indented
    3. void MainWindow:processPendingDatagrams() <- Notice no emoticon and colored text
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. QUdpSocket readyRead() signal not triggered
    By deionut in forum Newbie
    Replies: 2
    Last Post: 26th October 2010, 18:19
  2. QTcpSocket and readyRead and QTimer
    By cafu in forum Qt Programming
    Replies: 2
    Last Post: 18th December 2009, 13:36
  3. readyRead problem
    By fruzzo in forum Qt Programming
    Replies: 11
    Last Post: 2nd September 2009, 19:50
  4. QExtSerialPort with readyRead()
    By tho97 in forum Qt Programming
    Replies: 4
    Last Post: 27th August 2008, 20:18
  5. readyRead signal too slow or what?
    By Morea in forum Qt Programming
    Replies: 1
    Last Post: 6th July 2007, 19:11

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.