Results 1 to 6 of 6

Thread: QUdpsocket send and receive broadcast in linux

Threaded View

Previous Post Previous Post   Next Post Next Post
  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.

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.