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 localhost IP and gateway information ?

How to get localhost IP and gateway information ?

Scheduled Pinned Locked Moved C / C++ / MFC
tutorialquestion
5 Posts 4 Posters 2 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.
  • G Offline
    G Offline
    GflPower
    wrote on last edited by
    #1

    I want to get localhost's IP address ? I want to get localhost's gateway? GFLPOWER

    J 1 Reply Last reply
    0
    • G GflPower

      I want to get localhost's IP address ? I want to get localhost's gateway? GFLPOWER

      J Offline
      J Offline
      Juan Antonio Bermejo
      wrote on last edited by
      #2

      Try this: DWORD GetLocalIP() { static char sName[MAX_COMPUTERNAME_LENGTH + 1]; DWORD dwLen = sizeof(sName); GetComputerName(sName, &dwLen); HOSTENT *host = gethostbyname(sName); DWORD dwIP = 0; if(host->h_addr_list[0] != 0) { dwIP <<= 8; dwIP |= (BYTE)(host->h_addr_list[0][0]); dwIP <<= 8; dwIP |= (BYTE)(host->h_addr_list[0][1]); dwIP <<= 8; dwIP |= (BYTE)(host->h_addr_list[0][2]); dwIP <<= 8; dwIP |= (BYTE)(host->h_addr_list[0][3]); } return dwIP; }

      S 1 Reply Last reply
      0
      • J Juan Antonio Bermejo

        Try this: DWORD GetLocalIP() { static char sName[MAX_COMPUTERNAME_LENGTH + 1]; DWORD dwLen = sizeof(sName); GetComputerName(sName, &dwLen); HOSTENT *host = gethostbyname(sName); DWORD dwIP = 0; if(host->h_addr_list[0] != 0) { dwIP <<= 8; dwIP |= (BYTE)(host->h_addr_list[0][0]); dwIP <<= 8; dwIP |= (BYTE)(host->h_addr_list[0][1]); dwIP <<= 8; dwIP |= (BYTE)(host->h_addr_list[0][2]); dwIP <<= 8; dwIP |= (BYTE)(host->h_addr_list[0][3]); } return dwIP; }

        S Offline
        S Offline
        Steve S
        wrote on last edited by
        #3

        Bad assumption. You should use the ntohXX functions to convert from network format to host. You never know, one day you will write code that isn't destined to run on an x86 box. I migrated stuff between m68k, x86 and Sparc. Trust me on this, it's not fun. Steve S

        A 1 Reply Last reply
        0
        • S Steve S

          Bad assumption. You should use the ntohXX functions to convert from network format to host. You never know, one day you will write code that isn't destined to run on an x86 box. I migrated stuff between m68k, x86 and Sparc. Trust me on this, it's not fun. Steve S

          A Offline
          A Offline
          Alexander M
          wrote on last edited by
          #4

          ntohXX functions are very very slow, you shouldn't use them at all, I've written an own version with does the whole job with about 10 clocks. Don't try it, just do it! ;-)

          S 1 Reply Last reply
          0
          • A Alexander M

            ntohXX functions are very very slow, you shouldn't use them at all, I've written an own version with does the whole job with about 10 clocks. Don't try it, just do it! ;-)

            S Offline
            S Offline
            Steve S
            wrote on last edited by
            #5

            which, presumably, are using either handwritten assembly functions, or inline assembly? Which kind of defeats the purpose, which is platform-endian neutrality. I didn't say they were speed efficient, but they help you produce portable code. Steve S

            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