Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to find other machine's IP Address in Qt ??
Forum Updated to NodeBB v4.3 + New Features

How to find other machine's IP Address in Qt ??

Scheduled Pinned Locked Moved General and Desktop
8 Posts 3 Posters 2.3k Views 1 Watching
  • 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.
  • A Offline
    A Offline
    ankursaxena
    wrote on last edited by
    #1

    Hi All,

    I just want to know how to find ip Adrress of another machine programmatically in Qt.

    Any help will be appreciated.
    Thanx in Advance.

    1 Reply Last reply
    0
    • B Offline
      B Offline
      Binary91
      wrote on last edited by
      #2

      Hi,
      are you working with sockets? You have to catch the signal newConnection() for incoming connections and get the new QTcpSocket (nextPendingConnection()).
      With the QTcpSocket you can get the adress via peerAddress()...

      1 Reply Last reply
      0
      • A Offline
        A Offline
        ankursaxena
        wrote on last edited by
        #3

        Thank you @Binary91 for your response.

        I tried using sockets. but it takes sometime to find out all the address in a network.

        Then I combined sockets with multithreading to get the ip adress quickly. but In case if there are thousands of system , this procedure will take a lot of CPU usage and RAM. So I want to minimize it also.

        So Is there any other method to achieve this ?

        1 Reply Last reply
        0
        • S Offline
          S Offline
          Salvatello
          wrote on last edited by
          #4

          check this QNetworkInterface

          1 Reply Last reply
          0
          • A Offline
            A Offline
            ankursaxena
            wrote on last edited by
            #5

            I have used QNetworkInterface class but it is not very helpful as i needed to ipaddress of other machine or say (ip address of) all machines.

            1 Reply Last reply
            0
            • S Offline
              S Offline
              Salvatello
              wrote on last edited by
              #6

              you can try:

              @// Send a broadcast packet on all interfaces
              void DuktoProtocol::sendToAllBroadcast(QByteArray *packet)
              {
              // Get network interfaces list
              QList<QNetworkInterface> ifaces = QNetworkInterface::allInterfaces();

              // Interfaces iteration
              for (int i = 0; i < ifaces.size(); i++)
              {
                  // Now get all IP addresses for the current interface
                  QList<QNetworkAddressEntry> addrs = ifaces[i].addressEntries();
              
                  // And for any IP address, if it is IPv4 and the interface is active, send the packet
                  for (int j = 0; j < addrs.size(); j++)
                      if ((addrs[j].ip().protocol() == QAbstractSocket::IPv4Protocol) && (addrs[j].broadcast().toString() != ""))
                          mSocket->writeDatagram(packet->data(), packet->length(), addrs[j].broadcast(), UDP_PORT);
              }
              

              }@

              1 Reply Last reply
              0
              • A Offline
                A Offline
                ankursaxena
                wrote on last edited by
                #7

                Thank u @Salvatello for posting the code.
                I tried it but unfortunately it is not providing required output.

                This code is providing all kind of local Address of hosting machine.

                1 Reply Last reply
                0
                • A Offline
                  A Offline
                  ankursaxena
                  wrote on last edited by
                  #8

                  Hi andreyc!! Thank u for your reply.

                  As your suggestion, I am searching for mayne library on Windows. Will you please provide me the link for mayne libary.

                  Thank you.

                  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