I need to broadcast an udp packet on all network interfaces, using only QIODevice methods (QIODevice::write() and no QUDPSocket::writeDatagram()), apart from connetcing and binding.

I also have upgraded to qt 5.6 due to qt bug #26538.

A simplified example is as follows:
Qt Code:
  1. class Dialog : public QDialog
  2. {
  3. Q_OBJECT
  4.  
  5. public:
  6. explicit Dialog(QWidget *parent = 0);
  7. ~Dialog();
  8.  
  9. private:
  10. Ui::Dialog *ui;
  11. QUdpSocket *sock;
  12.  
  13. public slots:
  14. void onReadyRead(void);
  15. void onButtonClicked(void);
  16. };
  17.  
  18. #define USE_QIODEVICE // remove this define to use QUDPSocket methods
  19.  
  20. Dialog::Dialog(QWidget *parent) :
  21. QDialog(parent),
  22. ui(new Ui::Dialog)
  23. {
  24. ui->setupUi(this);
  25.  
  26. connect(ui->pushBtn,SIGNAL(clicked()), this, SLOT(onButtonClicked()));
  27.  
  28. qDebug() << qVersion();
  29.  
  30. sock = new QUdpSocket(this);
  31. connect(sock, &QUdpSocket::readyRead, this, &Dialog::onReadyRead);
  32.  
  33. #if !defined(USE_QIODEVICE)
  34. sock->bind();
  35. qDebug() << sock->localPort();
  36. #endif
  37. }
  38.  
  39. void Dialog::onButtonClicked(void)
  40. {
  41. QList<QNetworkInterface> list = QNetworkInterface::allInterfaces();
  42. foreach (QNetworkInterface iface, list)
  43. {
  44. QList<QNetworkAddressEntry> AEList = iface.addressEntries();
  45. foreach(QNetworkAddressEntry AE, AEList)
  46. {
  47. if(AE.broadcast().protocol() == QAbstractSocket::IPv4Protocol)
  48. {
  49. #if defined(USE_QIODEVICE)
  50. sock->bind(54366); // I have to call bind() for every interface because disconnectFromHost() unbind
  51. sock->connectToHost(AE.broadcast(), 44444); // I have to call connectToHost to make the subsequent call to write() works
  52. qDebug() << sock->localPort();
  53. sock->write("hallo");
  54. sock->disconnectFromHost(); // I have to call disconnectFromHost() otherwise the subsequent call to connectToHost() will fail
  55. #else
  56. dg = "hallo";
  57. sock->writeDatagram(dg,AE.broadcast(), 44444);
  58. #endif
  59. qDebug() << "send to" << AE.broadcast();
  60. }
  61. }
  62. }
  63. }
  64.  
  65. void Dialog::onReadyRead(void)
  66. {
  67. #if defined(USE_QIODEVICE)
  68.  
  69. qDebug() << "received";
  70. ba = sock->readAll();
  71. #else
  72. char data[64];
  73. quint16 port;
  74.  
  75. while(sock->hasPendingDatagrams())
  76. {
  77. sock->readDatagram(data, sizeof(data), &host, &port);
  78. qDebug() << "received";
  79. }
  80.  
  81. #endif
  82. // data processing...
  83. }
To copy to clipboard, switch view to plain text mode 

Unfortunately this code doesn't works because when the replies come back from the hosts the interface is (probably already) unbound, and onReadyRead() is never called.

When I remove definition of USE_QIODEVICE the QUDPSocket methods are used instead of QIODevice.
In this way all seems to work.

How can I get the wanted behaviour using only QIODevice methods (apart from connetcing and binding)?

Best regards
Max