Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Mobile and Embedded
  4. can not get the sender's IP with UdpSocket
Forum Updated to NodeBB v4.3 + New Features

can not get the sender's IP with UdpSocket

Scheduled Pinned Locked Moved Solved Mobile and Embedded
5 Posts 3 Posters 895 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    RunThiner
    wrote on last edited by
    #1

    hi, any reader.
    i am develop a udpsocket app on the win7 with Qt5.9.3 mingw.
    this app will send some data to the local network to find some embed linux device by udp and IPv4, the socket code as below:
    QUdpSocket *socket=new QUdpSocket(this);
    socket->open(QIODevice::ReadWrite);
    QHostAddress ad=QHostAddress(myIp);
    socket->bind(ad,port,QUdpSocket::ShareAddress|QAbstractSocket::ReuseAddressHint);

    //Send data:
    QHostAddress ad=QHostAddress(QHostAddress::Broadcast);
    QByteArray dat=QByteArray("Hello");
    socket->writeDatagram(dat.data(),dat.size(),ad,port);

    //Recv data:
    QHostAddress sndIp;
    quint16 uRcvPort;
    while(socket->hasPendingDatagrams()){
    qint64 l=recver->pendingDatagramSize();
    socket->readDatagram(dat.data(),l,&sndIp,&uRcvPort);
    }

    but the readDatagram do not fill any IP to the sndIp, the sndIp is NULL.

    but, if i change the bind to :socket->bind(port,QUdpSocket::ShareAddress|QAbstractSocket::ReuseAddressHint);
    the readDatagram will fill the sender's IP to the sndIP well.

    have any suggest for the QUdpSocket on the win7 64bits?

    thanks.

    aha_1980A 1 Reply Last reply
    0
    • R RunThiner

      hi, any reader.
      i am develop a udpsocket app on the win7 with Qt5.9.3 mingw.
      this app will send some data to the local network to find some embed linux device by udp and IPv4, the socket code as below:
      QUdpSocket *socket=new QUdpSocket(this);
      socket->open(QIODevice::ReadWrite);
      QHostAddress ad=QHostAddress(myIp);
      socket->bind(ad,port,QUdpSocket::ShareAddress|QAbstractSocket::ReuseAddressHint);

      //Send data:
      QHostAddress ad=QHostAddress(QHostAddress::Broadcast);
      QByteArray dat=QByteArray("Hello");
      socket->writeDatagram(dat.data(),dat.size(),ad,port);

      //Recv data:
      QHostAddress sndIp;
      quint16 uRcvPort;
      while(socket->hasPendingDatagrams()){
      qint64 l=recver->pendingDatagramSize();
      socket->readDatagram(dat.data(),l,&sndIp,&uRcvPort);
      }

      but the readDatagram do not fill any IP to the sndIp, the sndIp is NULL.

      but, if i change the bind to :socket->bind(port,QUdpSocket::ShareAddress|QAbstractSocket::ReuseAddressHint);
      the readDatagram will fill the sender's IP to the sndIP well.

      have any suggest for the QUdpSocket on the win7 64bits?

      thanks.

      aha_1980A Offline
      aha_1980A Offline
      aha_1980
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @RunThiner you are blocking the event loop with your while() loop.

      Use proper signals&slots to react on incoming datagrams.

      Qt has to stay free or it will die.

      1 Reply Last reply
      2
      • R Offline
        R Offline
        RunThiner
        wrote on last edited by
        #3

        I was found an other way to solved this problem: use the receiveDatagram, it will get the sender's IP back well:
        //recv data:
        QNetworkDatagram nDat=socket->receiveDatagram(socket->pendingDatagramSize());
        QHostaddress sndIp=nDat.senderAddress();

        i was tested on the ubuntu and win7,win10, the readDatagram and receiveDatagram can get the sender's IP on the ubuntu,
        but the readDatagram do not get the sender's IP on the win7 and win10.
        and the receiveDatagram can get the sender's IP on the win7 and win10.

        1 Reply Last reply
        0
        • dheerendraD Offline
          dheerendraD Offline
          dheerendra
          Qt Champions 2022
          wrote on last edited by
          #4

          It is a bug. You can refer here

          Dheerendra
          @Community Service
          Certified Qt Specialist
          http://www.pthinks.com

          1 Reply Last reply
          1
          • R Offline
            R Offline
            RunThiner
            wrote on last edited by
            #5

            got it, thanks.

            btw, i am test the QUdpSocket with QT5.12.0, the readDatagram does get the sender's ip back well.

            1 Reply Last reply
            0

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved