The problem with QNetwork: receiving UDP broadcast on android device
-
Actually I’d like to receive UDP broadcast on android device from other simple device (sender UDP broadcast). Both devices are in the same local network. The UDP datagrams are receiving from the Sender, but there is a problem with time of receiving… very long delaying – about 785 seconds! I sure, that the Sender has issued the UDP packet immediately (I have checked it by tcpdump on other computer in the same local network), but on the android device (which receiving UDP) the "bind" has send readReady signal only after the 785 seconds (exactly the same delaying for each datagram).
Environment: Qt 6.4, IBM-jdk-11.0.17+8, SDK android-31, NDK25.1.8937393, android device Huawei. (NOTE: Additionally I’v checked it with different version of Qt, JDK, SDK and NDK – but have the same result). Compilation without any errors/warnings. It seems to me, that there is some firewall in the Huawei device... May be I need to add some special permission in the androidmanifest? Please advice.For example the test project:
NOTE:- The "Sender" is sending the UDP datagram with time of issuing (format: "hh:mm:ss").
- This example code is working properly on the Ubuntu computer (It is receiving the UDP immediately).
- I'v checked with different model of mobile phone with the same result... but all of this mobile phone - is Huawei (Unfortunately, I have not any other vendor of phone for test).
TEMPLATE = app QT += widgets network SOURCES = main.cpp \ UdpClient.cpp HEADERS = UdpClient.h windows:TARGET = ../UdpClientUdpClient.h
#pragma once #include <QTextEdit> class QUdpSocket; class UdpClient : public QTextEdit { Q_OBJECT private: QUdpSocket* m_pudp; public: UdpClient(QWidget* pwgt = 0); private slots: void slotProcessDatagrams(); };main.cpp
#include <QApplication> #include "UdpClient.h" int main(int argc, char** argv) { QApplication app(argc, argv); UdpClient client; client.show(); return app.exec(); }UdpClient.cpp
#include <QtNetwork> #include <QtGui> #include "UdpClient.h" UdpClient::UdpClient(QWidget* pwgt /*=0*/) : QTextEdit(pwgt) { setWindowTitle("UdpClient"); m_pudp = new QUdpSocket(this); m_pudp->bind(QHostAddress::AnyIPv4, 49100, QUdpSocket::ShareAddress); connect(m_pudp, SIGNAL(readyRead()), SLOT(slotProcessDatagrams())); append("Test UDP bind:"); } void UdpClient::slotProcessDatagrams() { QByteArray baDatagram; do { baDatagram.resize(m_pudp->pendingDatagramSize()); m_pudp->readDatagram(baDatagram.data(), baDatagram.size()); } while(m_pudp->hasPendingDatagrams()); QDateTime dateTime; QTime Time; qint64 i_dTime; QTime sentTime; dateTime = QDateTime::currentDateTime(); Time = dateTime.time(); sentTime = QTime::fromString(QString::fromUtf8(baDatagram) ,"hh:mm:ss"); i_dTime = sentTime.secsTo(Time); append("R"+ QString::number(baDatagram.size()) + "b:" + Time.toString("hh:mm:ss") + "-" + QString(baDatagram) + "=" + QString::number(i_dTime)); }