PDA

View Full Version : Not getting readyRead from a QUdpSocket



evelBist
20th July 2010, 18:28
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::processPendingDatagrams()
{
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?

squidge
20th July 2010, 19:02
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.

evelBist
20th July 2010, 19:13
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?

squidge
20th July 2010, 19:43
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.


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

[code]
like this
indented
void MainWindow:processPendingDatagrams() <- Notice no emoticon and colored text