Results 1 to 6 of 6

Thread: QUdpsocket send and receive broadcast in linux

  1. #1
    Join Date
    Mar 2010
    Posts
    77
    Thanks
    10
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default QUdpsocket send and receive broadcast in linux

    I try to write a QUdpsocket for read and write on braodcast address. following code work in windows with Qt5 library but don't work on my CentOs with Qt4.6, (i haven't any error in CentOs just don't read anything)

    configure qudpsocket

    Qt Code:
    1. iface = "";
    2.  
    3. QList<QNetworkInterface> ifaces = QNetworkInterface::allInterfaces();
    4. foreach (QNetworkInterface nf, ifaces) {
    5. if(nf.name() == iface || iface.isNull() || iface.isEmpty())
    6. {
    7. if(nf.flags() & QNetworkInterface::CanBroadcast)
    8. {
    9. foreach (QNetworkAddressEntry na, nf.addressEntries()) {
    10. if(na.broadcast().toString() != "") {
    11. send_to = na.broadcast().toIPv4Address();
    12. bind_to = na.ip().toIPv4Address();
    13. hostname = na.ip().toString();
    14. if (verbose)
    15. qDebug("using address=%s broadcast=%s",
    16. na.ip().toString().toLocal8Bit().data(), na.broadcast().toString().toLocal8Bit().data());
    17. break;
    18. }
    19. }
    20. }
    21. }
    22. if(hostname != "") break;
    23. }
    24.  
    25. if(bind_to)
    26. {
    27. broadcast.setAddress(send_to);
    28. #ifdef Q_OS_WIN
    29. QHostAddress address(bind_to);
    30. #elif defined(Q_OS_LINUX)
    31. QHostAddress address(send_to); // dont work on centos Qt 4.7
    32. #endif
    33.  
    34. if(!udpsock.bind(address, port_nbr, QUdpSocket::ShareAddress
    35. | QUdpSocket::ReuseAddressHint))
    36. qFatal("bind udp");
    37.  
    38. if(verbose)
    39. qDebug() << "configured, hostname=" << hostname;
    40. }
    To copy to clipboard, switch view to plain text mode 
    loop for listening
    Qt Code:
    1. int handleUdp() {
    2. if(!udpsock.hasPendingDatagrams()) return -1;
    3. char data[1024];
    4. QHostAddress peerAddress;
    5.  
    6. int rsize = udpsock.readDatagram(data, 1024, &peerAddress);
    7. if(rsize <= 0) return -1;
    8. .....
    9. return 0;
    10. }
    11.  
    12. while(!terminated) {
    13. ...
    14. if(udpsock.waitForReadyRead(2000))
    15. handleUdp();
    16. ...
    17. // end of each loop broadcast data
    18. udpsock.writeDatagram(data(), broadcast, port_nbr)
    19. }
    To copy to clipboard, switch view to plain text mode 

    this is debug information:

    using address=123.123.1.14 broadcast=123.255.255.255
    configured, hostname=123.123.1.14
    Last edited by danics; 2nd February 2015 at 13:20.

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

    Default Re: QUdpsocket send and receive broadcast in linux

    Why do you think you should bind(line 34) to a broadcast address (line 31) on the Linux machine?

  3. #3
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,420
    Thanks
    37
    Thanked 1,545 Times in 1,495 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QUdpsocket send and receive broadcast in linux

    Check the port number.
    Maybe you are accidentally trying to bind to a port < 1024

    Cheers,
    _

  4. #4
    Join Date
    Mar 2010
    Posts
    77
    Thanks
    10
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QUdpsocket send and receive broadcast in linux

    Quote Originally Posted by ChrisW67 View Post
    Why do you think you should bind(line 34) to a broadcast address (line 31) on the Linux machine?
    I read this in different forums for example here and see in other opensource too, however binding to IP address dont resolve problem anyway.

    I listen to a port bigger than 5000

  5. #5
    Join Date
    Mar 2010
    Posts
    77
    Thanks
    10
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QUdpsocket send and receive broadcast in linux

    I test this code in ubuntu with Qt 5 library and it works.

    so i think maybe problem is network settings of my centos, actually my centos run in a virtual machine, anybody have a suggestion of changing settings of network for broadcast messages localy?

    thanks in advance

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

    Default Re: QUdpsocket send and receive broadcast in linux

    If you are using a virtual machine and you want it to talk to the outside world and accept connections/UDP from the outside world then you need to choose an appropriate virtual network interface option: usually "Bridged" or something like it. On the other hand, two virtual machines connected only to a shared VM-only network should require nothing special to talk to each other. Also need to check there is no default network firewalling going on in the OS.

Similar Threads

  1. QUdpSocket & broadcast & Windows7
    By xoz in forum General Programming
    Replies: 1
    Last Post: 27th October 2011, 10:08
  2. Using QUdpSocket to detect peripheral via a broadcast
    By Eos Pengwern in forum Qt Programming
    Replies: 4
    Last Post: 9th November 2010, 11:21
  3. QUdpSocket broadcast to multiple processes
    By tomschuring in forum Qt Programming
    Replies: 1
    Last Post: 30th September 2010, 01:33
  4. Qt/S60 QUdpSocket broadcast problem
    By harnen in forum Qt for Embedded and Mobile
    Replies: 0
    Last Post: 13th April 2010, 22:13
  5. how to send broadcast messages in networkadapters
    By jeo in forum Qt Programming
    Replies: 3
    Last Post: 26th March 2009, 10:17

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.