Results 1 to 7 of 7

Thread: UDP Listener

  1. #1
    Join Date
    Jan 2014
    Posts
    3
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Default UDP Listener

    I am working with a piece of custom hardware (black box) that is sending UDP packets of data. Looking at the packets in Wireshark I can see that the hardware has the source IP set to 192.168.1.128 on port 4096 and the destination IP set to 0.0.0.0 on port 8192.

    I am running a small Qt application on a Windows 7 machine that opens a UDP listener on port 8192 and should display packet data when it is received but, it never receives a packet. At this point I don't care what the data looks like I just want to verfiy that I can receive UDP from the external hardware.

    Is this an operating system issue such that Windows will not receive a packet with destination set to 0.0.0.0? Should I use winpcap lib?

    Any insight is most appreciated.

    I have included an example of the UDP listener code.

    Qt Code:
    1. Gui::Gui(QWidget *parent)
    2. : QMainWindow(parent){
    3. ui.setupUi(this);
    4.  
    5. udpSocket = new QUdpSocket(this);
    6.  
    7. connect(udpSocket, SIGNAL(readyRead()), this, SLOT(processPendingDatagrams()));
    8.  
    9. udpSocket->bind(8192);
    10.  
    11. setWindowTitle(tr("Display"));
    12. }
    13.  
    14. void SeekerGui::processPendingDatagrams()
    15. {
    16. while (udpSocket->hasPendingDatagrams())
    17. {
    18. QByteArray datagram;
    19.  
    20. datagram.resize(udpSocket->pendingDatagramSize());
    21.  
    22. udpSocket->readDatagram(datagram.data(), datagram.size());
    23.  
    24. ui.lineEdit_test->setText(tr("Received datagram: \"%1\"").arg(datagram.data()));
    25. }
    26. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: UDP Listener

    Have you ensured the Windows (or other) firewall is not blocking the incoming packet or your application as a whole?
    Have you tried specifying the interface to bind() to on the receiver?

  3. #3
    Join Date
    Jan 2014
    Posts
    3
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Default Re: UDP Listener

    I made sure that the firewall was disabled during testing.

    I'm not sure what you mean by, specifying the interface to bind to.

  4. #4
    Join Date
    Jan 2009
    Location
    The Netherlands and Spain
    Posts
    150
    Thanks
    6
    Thanked 18 Times in 18 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: UDP Listener

    You connected the SIGNAL to this.processPendingDatagrams(), while you're showing processPendingDatagrams() as a method in class SeekerGui?

  5. #5
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: UDP Listener

    Indeed, missed that too.

  6. #6
    Join Date
    Jan 2014
    Posts
    3
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Default Re: UDP Listener

    Sorry. SeekerGui should read Gui. The processPendingDatagrams() method resides in the Gui class. My apologies.

    Here is the code snippet the way it should read.

    Qt Code:
    1. Gui::Gui(QWidget *parent) : QMainWindow(parent)
    2. {
    3. ui.setupUi(this);
    4.  
    5. udpSocket = new QUdpSocket(this);
    6.  
    7. connect(udpSocket, SIGNAL(readyRead()), this, SLOT(processPendingDatagrams()));
    8.  
    9. udpSocket->bind(8192);
    10.  
    11. setWindowTitle(tr("Display"));
    12.  
    13. }
    14.  
    15. void Gui::processPendingDatagrams()
    16. {
    17. while (udpSocket->hasPendingDatagrams())
    18. {
    19. QByteArray datagram;
    20.  
    21. datagram.resize(udpSocket->pendingDatagramSize());
    22.  
    23. udpSocket->readDatagram(datagram.data(), datagram.size());
    24.  
    25. ui.lineEdit_test->setText(tr("Received datagram: \"%1\"").arg(datagram.data()));
    26. }
    27. }
    To copy to clipboard, switch view to plain text mode 

  7. #7
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: UDP Listener

    Does the connect call return true?
    Is the code in processPendingDatagrams() ever called?

    There is a bind() variant that takes two arguments (see the example in the QUdpSocket). The first argument specifies which interface the socket is created on. Have you tried that with the external interface address specified explicitly?

Similar Threads

  1. Replies: 12
    Last Post: 18th April 2011, 16:24
  2. Problem with Listener and QPixmap
    By thiagotrss in forum Qt Programming
    Replies: 2
    Last Post: 13th October 2009, 19:24
  3. Phone call listener
    By raulftang in forum Qt for Embedded and Mobile
    Replies: 1
    Last Post: 25th April 2007, 20:09

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.