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. Detecting Local IP Address

Detecting Local IP Address

Scheduled Pinned Locked Moved C / C++ / MFC
sysadmintutorial
4 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.
  • W Offline
    W Offline
    wasife
    wrote on last edited by
    #1

    Hi, I want to know my local ip address. I am on a local area network. I have used the following code: char* hostName=new char[255]; if(::gethostname(hostName,255)==0) { hostent* localhost=::gethostbyname(hostName); ULONG ip=inet_addr(*(localhost->h_addr_list)); in_addr localaddr; localaddr.S_un.S_addr=ip; char* localIP=inet_ntoa(localaddr); CString localAddress(localIP); AfxMessageBox(localAddress); } It always give the 255.255.255.255 as my local address. Can anybody tell me how to get my own IP. I dont want to use iphelp or any other advanced APIs. Regards, Wasif.

    E M M 3 Replies Last reply
    0
    • W wasife

      Hi, I want to know my local ip address. I am on a local area network. I have used the following code: char* hostName=new char[255]; if(::gethostname(hostName,255)==0) { hostent* localhost=::gethostbyname(hostName); ULONG ip=inet_addr(*(localhost->h_addr_list)); in_addr localaddr; localaddr.S_un.S_addr=ip; char* localIP=inet_ntoa(localaddr); CString localAddress(localIP); AfxMessageBox(localAddress); } It always give the 255.255.255.255 as my local address. Can anybody tell me how to get my own IP. I dont want to use iphelp or any other advanced APIs. Regards, Wasif.

      E Offline
      E Offline
      evilsocket
      wrote on last edited by
      #2

      Assuming that you have only one network interface (your eth card) and a loopback interface (127.0.0.1), i think you could try to get the name of the local computer, and then resolve it in an ip address . char szMyComputerName[0xFF] = {0}, szMyIpAddress[0xFF] = {0}; DWORD dwSize = 0xFF; ::GetComputerName(szMyComputerName,&dwSize); Now all you have to to is to resolve 'szMyComputerName' as you did in your example . Naturally, if you have more than one network interface (eth card, wireless, etc), you have to enumerate them and then you choose the one you want to work on : SOCKET sd = WSASocket(AF_INET, SOCK_DGRAM, 0, 0, 0, 0); INTERFACE_INFO InterfaceList[20]; unsigned long nBytesReturned; WSAIoctl( sd, SIO_GET_INTERFACE_LIST, 0, 0, &InterfaceList, sizeof(InterfaceList), &nBytesReturned, 0, 0); for( int i = 0; i < nBytesReturned / sizeof(INTERFACE_INFO); i++ ) { printf( "Interface[%d] : %s\n",i, inet_ntoa( ((sockaddr_in *)&(InterfaceList[i].iiAddress))->sin_addr ) ) } Hope i helped ....:)

      1 Reply Last reply
      0
      • W wasife

        Hi, I want to know my local ip address. I am on a local area network. I have used the following code: char* hostName=new char[255]; if(::gethostname(hostName,255)==0) { hostent* localhost=::gethostbyname(hostName); ULONG ip=inet_addr(*(localhost->h_addr_list)); in_addr localaddr; localaddr.S_un.S_addr=ip; char* localIP=inet_ntoa(localaddr); CString localAddress(localIP); AfxMessageBox(localAddress); } It always give the 255.255.255.255 as my local address. Can anybody tell me how to get my own IP. I dont want to use iphelp or any other advanced APIs. Regards, Wasif.

        M Offline
        M Offline
        mahw pgm
        wrote on last edited by
        #3

        wasife wrote:

        ULONG ip=inet_addr(*(localhost->h_addr_list)); in_addr localaddr; localaddr.S_un.S_addr=ip; char* localIP=inet_ntoa(localaddr);

        replace ur code above with line below: char* localIP=inet_ntoa(*(LPIN_ADDR)*(localhost->h_addr_list)); It should be work, but I can't tell why ur code doesn't. wish this could help!

        1 Reply Last reply
        0
        • W wasife

          Hi, I want to know my local ip address. I am on a local area network. I have used the following code: char* hostName=new char[255]; if(::gethostname(hostName,255)==0) { hostent* localhost=::gethostbyname(hostName); ULONG ip=inet_addr(*(localhost->h_addr_list)); in_addr localaddr; localaddr.S_un.S_addr=ip; char* localIP=inet_ntoa(localaddr); CString localAddress(localIP); AfxMessageBox(localAddress); } It always give the 255.255.255.255 as my local address. Can anybody tell me how to get my own IP. I dont want to use iphelp or any other advanced APIs. Regards, Wasif.

          M Offline
          M Offline
          Mark Salsbery
          wrote on last edited by
          #4

          Try this (works on my machines)

          char* hostName=new char[255];
          if (0 == ::gethostname(hostName, 255))
          {
          HOSTENT *pHostent = ::gethostbyname(szTempName);
          if (pHostent && pHostent->h_addrtype == AF_INET)
          {
          in_addr inaddr = *(in_addr *)(pHostent->h_addr_list[0]);
          char *pAddStr = ::inet_ntoa(inaddr);
          if (pAddStr)
          {
          AfxMessageBox(pAddStr);
          }
          }
          }

          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