Results 1 to 5 of 5

Thread: Using QUdpSocket to detect peripheral via a broadcast

  1. #1
    Join Date
    Aug 2009
    Posts
    50
    Thanks
    9
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Using QUdpSocket to detect peripheral via a broadcast

    As the title suggests, I'm trying to use a QUdpSocket to detect a peripheral which is attached to the host computer. The desired sequence-of-events is:

    1. Host broadcasts an "Is anybody there" message to its whole network.
    2. The peripheral, on receiving the broadcast, sends a message back saying "here I am".
    3. The host receives the response, and checks whether it is a valid "here I am" message; if not, it ignores it and waits for the next message; if it is, then it makes a notes of the sender's address so further communications can be addressed exclusively to the peripheral.

    Here's my code:

    Qt Code:
    1. // The QByteArray "datagram" initially contains the "Is anybody there" message
    2. QUdpSocket udpSocket;
    3. udpSocket.bind(QHostAddress::Any, 2101);
    4. udpSocket.writeDatagram(datagram.data(), datagram.size(),
    5. QHostAddress::Broadcast, 2101);
    6.  
    7. datagram.resize(sizeof(DISCOVERY_RESPONSE));
    8. QHostAddress senderAddress;
    9. quint16 senderPort;
    10. QTime timeout_counter;
    11. timeout_counter.start();
    12. while (timeout_counter.elapsed() < TIMEOUT)
    13. {
    14. if (udpSocket.hasPendingDatagrams())
    15. {
    16. int returnedSize = udpSocket.readDatagram(datagram.data(), datagram.size(),
    17. &senderAddress, &senderPort);
    18. int responseSize = sizeof(DISCOVERY_RESPONSE);
    19. if (returnedSize == responseSize)
    20. {
    21. // Great joy
    22. DISCOVERY_RESPONSE *response = reinterpret_cast<DISCOVERY_RESPONSE *>(datagram.data());
    23. }
    24. }
    25. else QCoreApplication::processEvents();
    26. }
    To copy to clipboard, switch view to plain text mode 

    What actually happens is that the "udpSocket.readDatagram" line immediately receives the host's own broadcast but, realising that this isn't what its looking for, discards it and carries on listening. The remote peripheral, meanwhile, receives the broadcast and responds to it. However, the QUdpSocket does not received the response, it just carries on looping till it times out.

    If I happen to know the peripheral's IP address already (for example, by interrogating my router) and I hard-code this addess in place of QHostAddress::Broadcast in the above code, then everything behaves perfectly. It's as if, having once received data from one address (i.e. its own broadcast), the QUdpSocket has become blind to data sent from anywhere else, despit having been bound at the beginning to QHostAddress::Any. Is there a way that I can reset it, or is there something else that I'm doing wrong here?

    Thanks,
    Stephen.


    Added after 8 minutes:


    How odd...

    If I disable my firewall, then it works properly. Yet even with the firewall in place, the broadcast message got out to the peripheral, and the message from the peripheral was getting back whenever it had been addressed directly as opposed to being addressed via a broadcast.

    It's as if the firewall shuts down incoming messages whenever it detects that a broadcast has taken place. Very antisocial.

    So, today I've learned nothing about QUdpSockets but plenty about Firewalls.

    Stephen.
    Last edited by Eos Pengwern; 8th November 2010 at 15:28.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Using QUdpSocket to detect peripheral via a broadcast

    It's usually a good idea to be standard conformant whenever you can, especially when there are already required libraries available.

    http://en.wikipedia.org/wiki/Zeroconf
    http://www.dns-sd.org/
    http://doc.trolltech.com/qq/qq23-bonjour.html
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Aug 2009
    Posts
    50
    Thanks
    9
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Using QUdpSocket to detect peripheral via a broadcast

    Thank you. What I'm doing is intended to be compliant with the GigE Vision standard for cameras and other optical devices, hence the use of UDP rather than TCP/IP. I'm not aware of anything available off-the-shelf for this, but I do remember having serious firewall problems when using a Dalsa GigE Vision camera with the vendor's own software a couple of years ago - basically I had to disable the firewall whenever I wanted to use the camera. I'd forgotten all about it until today. Maybe I should just use different firewall software.

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Using QUdpSocket to detect peripheral via a broadcast

    Quote Originally Posted by Eos Pengwern View Post
    Thank you. What I'm doing is intended to be compliant with the GigE Vision standard for cameras and other optical devices,
    I don't think the standard deals with service discovery. You can still use bonjour/zeroconf for that.

    hence the use of UDP rather than TCP/IP.
    Who's talking about TCP here?

    basically I had to disable the firewall whenever I wanted to use the camera. I'd forgotten all about it until today. Maybe I should just use different firewall software.
    Decent firewalls handle UPnP connections well, that's a standard too.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. The following user says thank you to wysota for this useful post:

    Eos Pengwern (9th November 2010)

  6. #5
    Join Date
    Mar 2010
    Location
    Gdynia, Poland
    Posts
    12
    Thanked 3 Times in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Using QUdpSocket to detect peripheral via a broadcast

    Hi

    First of all when you're using QHostAddress::Broadcast it means that you're datagrams to 255.255.255.255 IP address and they will be received only in your LAN segment. It means that if your devices are somewhere outside your LAN they do not receive anything.

Similar Threads

  1. QUdpSocket broadcast to multiple processes
    By tomschuring in forum Qt Programming
    Replies: 1
    Last Post: 30th September 2010, 02:33
  2. Qt/S60 QUdpSocket broadcast problem
    By harnen in forum Qt for Embedded and Mobile
    Replies: 0
    Last Post: 13th April 2010, 23:13
  3. Broadcast string with udpsocket
    By wirasto in forum Qt Programming
    Replies: 5
    Last Post: 23rd September 2009, 12:13
  4. broadcast ip interface on windows xp
    By pawer in forum Qt Programming
    Replies: 14
    Last Post: 19th May 2007, 16:49
  5. Qt4 - UDP Broadcast
    By guilugi in forum Qt Programming
    Replies: 1
    Last Post: 5th July 2006, 15:36

Tags for this Thread

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.