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. Finding local and public IP address with Qt?
Forum Updated to NodeBB v4.3 + New Features

Finding local and public IP address with Qt?

Scheduled Pinned Locked Moved Unsolved General and Desktop
11 Posts 2 Posters 8.8k Views 2 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.
  • P Offline
    P Offline
    Pippin
    wrote on last edited by
    #1

    Hello,

    how exactly am I supposed to get my local IP address with Qt? my public IP address? The following gets a list of IP addresses, but I don't know what to do with it afterward:

    auto list = QHostInfo::fromName(QHostInfo::localHostName()).addresses();
    

    Thanks in advance.

    1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi
      It returns a list.
      QList<QHostAddress> list = QHostInfo::fromName(QHostInfo::localHostName()).addresses();
      for (int var = 0; var < list.size(); ++var) {
      qDebug() << list[var].XXXXXX();
      }
      XXXXX being what you are interested in;

      1 Reply Last reply
      0
      • P Offline
        P Offline
        Pippin
        wrote on last edited by
        #3

        @mrjj Okay but what is list[0].toString()? what is list[1].toString()? Are these strings of my local or public IP address? What's the size of that list?

        mrjjM 1 Reply Last reply
        0
        • P Pippin

          @mrjj Okay but what is list[0].toString()? what is list[1].toString()? Are these strings of my local or public IP address? What's the size of that list?

          mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @Pippin
          Its a list of QHostAddress objects.
          http://doc.qt.io/qt-5.5/qhostaddress.html

          this size of list is how many QHostAddress there is in list.

          1 Reply Last reply
          0
          • P Offline
            P Offline
            Pippin
            wrote on last edited by
            #5

            I did understand that, but I was asking my question in this particular instance:

            auto list = QHostInfo::fromName(QHostInfo::localHostName()).addresses();
            

            Since the local host is my computer, why should it (or any other computer for that matter) have more than 1 IP address? Why should there be a list at all? Could you explain to me how I am supposed to get my local IP address with this list? And how I'm supposed to get my public IP address? I'm a bit confused.

            1 Reply Last reply
            0
            • mrjjM Offline
              mrjjM Offline
              mrjj
              Lifetime Qt Champion
              wrote on last edited by mrjj
              #6

              Hi
              A pc can have many interfaces. Like one for lan , one for wifi.
              You could also have more than one netcard.
              So that's why it's a list.
              Not sure how you would get local iP from a QHostAddress.

              People seems to use other calls:
              http://stackoverflow.com/questions/12927273/how-to-get-local-ip-address-of-a-computer-using-qt

              Update:
              toString() seems to return the iP
              i get
              "fe80::a102:d348:5850:49c1"
              "fe80::5497:3dfa:7a29:4910"
              "fe80::b84a:b6cc:fe61:6662"
              "192.168.1.33" <<<<<<<<<<<<<< this is my real one.
              "192.168.13.1"
              "192.168.195.1"

              Update2:
              For your WAN (public IP)
              please see here
              https://forum.qt.io/topic/12466/solved-hostinfo-h-how-to-get-the-right-ip-address/4
              the
              QNetworkReply reply = accessManager->get(QNetworkRequest("http://whatismyip.org/"));
              post.

              1 Reply Last reply
              0
              • P Offline
                P Offline
                Pippin
                wrote on last edited by
                #7

                Okay thanks for your help. I'm just so surprised/disappointed that it didn't seem to occur to Qt's coding team that we might want to know our own IP addresses... Every single solution looks like a Rube Goldberg machine.

                mrjjM 1 Reply Last reply
                0
                • P Pippin

                  Okay thanks for your help. I'm just so surprised/disappointed that it didn't seem to occur to Qt's coding team that we might want to know our own IP addresses... Every single solution looks like a Rube Goldberg machine.

                  mrjjM Offline
                  mrjjM Offline
                  mrjj
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  @Pippin
                  well there is not easy / straight way to get IP
                  as its not how it works.

                  Most PC have multiple interfaces (and hence ips)
                  and none can tell which is the correct one in just a single call.

                  For external IP, these is no way to know except to look it up like
                  linked showed.

                  Network is inherently complex and getting IP is small part of using the network classes among the
                  1000 other things it also must do.

                  1 Reply Last reply
                  0
                  • P Offline
                    P Offline
                    Pippin
                    wrote on last edited by Pippin
                    #9

                    @mrjj It doesn't seem that complex for the people running the SFML: see their page.

                    Both the local and public addresses are only a static function away with the SFML. I wasn't expecting things to get over-complicated without this library. I'm probably going to use it for network purposes after all.

                    1 Reply Last reply
                    0
                    • mrjjM Offline
                      mrjjM Offline
                      mrjj
                      Lifetime Qt Champion
                      wrote on last edited by
                      #10

                      Have you tried it ?
                      It most likely just return the first found.
                      Anyway, Qt is much, much bigger than SFML so you cannot expect
                      all things to be as neatly wrapped up as SFML.

                      But why don't you just use SFML if it suits better?

                      P 1 Reply Last reply
                      0
                      • mrjjM mrjj

                        Have you tried it ?
                        It most likely just return the first found.
                        Anyway, Qt is much, much bigger than SFML so you cannot expect
                        all things to be as neatly wrapped up as SFML.

                        But why don't you just use SFML if it suits better?

                        P Offline
                        P Offline
                        Pippin
                        wrote on last edited by Pippin
                        #11

                        @mrjj I do think I'm about to use the SFML. I initially wanted to use Qt as much as possible so that I don't have to link anything non-Qt to my project. I agree, though, with your other point: I've noticed how practical and extensive Qt can get, as opposed to the SFML. It's just sad to see that the SFML is sometimes much easier to use for very straightforward issues.

                        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