Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. how to get ip address of a host

how to get ip address of a host

Scheduled Pinned Locked Moved C / C++ / MFC
questionsysadminhelptutorial
7 Posts 4 Posters 0 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.
  • M Offline
    M Offline
    Member 10438623
    wrote on last edited by
    #1

    The problem is that a host could have lots of network interfaces, and an interface could be bound to more than one IP address. And to top that, not all IP addresses will be reachable from off the machine. Some could be virtual devices, and others could be private network IP addresses. How do i know my local ip address which is currently reachable from outside network?

    L A 2 Replies Last reply
    0
    • M Member 10438623

      The problem is that a host could have lots of network interfaces, and an interface could be bound to more than one IP address. And to top that, not all IP addresses will be reachable from off the machine. Some could be virtual devices, and others could be private network IP addresses. How do i know my local ip address which is currently reachable from outside network?

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      Is this something to do with C++?

      Veni, vidi, abiit domum

      M 1 Reply Last reply
      0
      • L Lost User

        Is this something to do with C++?

        Veni, vidi, abiit domum

        M Offline
        M Offline
        Member 10438623
        wrote on last edited by
        #3

        Yes, i am looking for a C Program for this...

        L A 2 Replies Last reply
        0
        • M Member 10438623

          Yes, i am looking for a C Program for this...

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          Then you should be searching Google for samples. Please read http://www.codeproject.com/Messages/2922875/HOW-TO-ASK-A-QUESTION.aspx[^].

          Veni, vidi, abiit domum

          1 Reply Last reply
          0
          • M Member 10438623

            Yes, i am looking for a C Program for this...

            A Offline
            A Offline
            ahmad_ali
            wrote on last edited by
            #5

            Hi! One of the few solutions that work relibaly are based on establishing a connection to some external website (e.g. google.com) and then checking the ip address of the local endpoint. Example: http://stackoverflow.com/questions/2674314/get-local-ip-address-using-boost-asio[^]

            M 1 Reply Last reply
            0
            • M Member 10438623

              The problem is that a host could have lots of network interfaces, and an interface could be bound to more than one IP address. And to top that, not all IP addresses will be reachable from off the machine. Some could be virtual devices, and others could be private network IP addresses. How do i know my local ip address which is currently reachable from outside network?

              A Offline
              A Offline
              arishri
              wrote on last edited by
              #6

              char cNamearr[255];
              gethostname(cNamearr, 255);
              hostent* hp = gethostbyname(cNamearr);

              \_sockaddr.sin\_addr.s\_addr   = \_nAdapterIp;//inet\_addr((const char\*)\_ucIpCharArr);
              \_sockaddr.sin\_addr.S\_un.S\_un\_b.s\_b1 = hp->h\_addr\_list\[0\]\[0\];
              \_sockaddr.sin\_addr.S\_un.S\_un\_b.s\_b2 = hp->h\_addr\_list\[0\]\[1\];
              \_sockaddr.sin\_addr.S\_un.S\_un\_b.s\_b3 = hp->h\_addr\_list\[0\]\[2\];
              \_sockaddr.sin\_addr.S\_un.S\_un\_b.s\_b4 = hp->h\_addr\_list\[0\]\[3\];
              
              1 Reply Last reply
              0
              • A ahmad_ali

                Hi! One of the few solutions that work relibaly are based on establishing a connection to some external website (e.g. google.com) and then checking the ip address of the local endpoint. Example: http://stackoverflow.com/questions/2674314/get-local-ip-address-using-boost-asio[^]

                M Offline
                M Offline
                Member 10438623
                wrote on last edited by
                #7

                Thanks Ahmad. But I have one doubt in the above solution. Suppose there are two ips configured in the system(one is through LAN and other one through USB) and we can connect external ip through one of the local ips(say through USB). Then does this solution always returns the local ip which can connect outside(USB in this case) or it will return any one ip.

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups