Send data to Qt application(Laptop) from Android using UDP

谁说我不能喝 提交于 2020-12-15 05:02:02

问题


In my Qt5 application, I'm trying to implement UDP :

    port = _M_PORT;
    socket = new QUdpSocket(this);
    //socket->bind(QHostAddress("192.168.0.100"),port);
    socket->bind(QHostAddress("192.168.0.108"),port);

    connect(socket,SIGNAL(readyRead()),this,SLOT(readyRead()));

Also tried :

    socket = new QUdpSocket(this);
    socket->connectToHost("192.168.0.108",port);
    socket->bind(port);

IP address of my PC is 192.168.0.108 and IP address of my Android phone is 192.168.0.100.

I'm reading datagrams from UDP like this :

void ExternalCamera::readyRead(){
    QByteArray Buffer;
    Buffer.resize(socket->pendingDatagramSize());

    QHostAddress sender;
    quint16 senderPort;
    socket->readDatagram(Buffer.data(),Buffer.size(),&sender,&senderPort);
}

Then I'm working with these Buffer.data()

When I'm sending data like this to 127.0.0.1/8008 works. But if I send data to 192.168.0.108 it doesn't work. So now how am I supposed to send data from my Android to Qt ?

Both device on a same network, connected via WiFi.

And I've verified that data is coming to Laptop from Android, using :

sudo tcpdump -n udp port 8008

Output was :

tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on enp2s0f2, link-type EN10MB (Ethernet), capture size 262144 bytes
16:13:54.115815 IP 192.168.0.100.49858 > 192.168.0.108.8008: UDP, length 50
16:13:54.116912 IP 192.168.0.100.48310 > 192.168.0.108.8008: UDP, length 49
16:13:54.165180 IP 192.168.0.100.41370 > 192.168.0.108.8008: UDP, length 49
16:13:54.167097 IP 192.168.0.100.38006 > 192.168.0.108.8008: UDP, length 50
...

I even opened the port :

maifee@maifee-ubuntu:~$ sudo ufw verbose
[sudo] password for maifee: 
Status: active

To                         Action      From
--                         ------      ----
80/tcp                     ALLOW       Anywhere                  
443/tcp                    ALLOW       Anywhere                  
8008/tcp                   ALLOW       Anywhere                  
8008/udp                   ALLOW       Anywhere                  
80/tcp (v6)                ALLOW       Anywhere (v6)             
443/tcp (v6)               ALLOW       Anywhere (v6)             
8008/tcp (v6)              ALLOW       Anywhere (v6)             
8008/udp (v6)              ALLOW       Anywhere (v6)             

maifee@maifee-ubuntu:~$ 

来源:https://stackoverflow.com/questions/65232467/send-data-to-qt-applicationlaptop-from-android-using-udp

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!