IP Enumeration

.

Environment: VC4, VC5, VC6

I think this is the quickest way to enumerate the available IP addresses on your machine. I've gotten this question a lot. This is fairly simple but haven't seen it anywhere.

IP Enumeration

// Function GetIPs
// in : CStringArray
// out : CStringArray + count

int GetIPs( CStringArray &arrIPS )
{
    BOOL bRet = TRUE;
    char name[255];
    int i=0;
    PHOSTENT hostinfo;

  //first, clear the list
    arrIPS.RemoveAll();

  //get the host name
  if( gethostname ( name, sizeof(name)) == 0)
  {
    // get the host info
    if((hostinfo = gethostbyname(name)) != NULL)
    {
      // now, loop until the address list = null
      while( hostinfo->h_addr_list[i] != NULL )
      {
        //get the IP address
        char *ip_addr = inet_ntoa (*(struct in_addr *)
                        hostinfo->h_addr_list[i]);
        //add it to the array
        ip_addr );
        //increment the counter
        i++;
      }
    }
  }
  //return the array count
  return arrIPS.GetSize();
}


Comments

  • Don't forget to call WSAStart()...

    Posted by Legacy on 12/30/2003 12:00am

    Originally posted by: David Little

    This fails if you don't call WSAStart() first.  And the missing code is:
    
    


    arrIPS.Add(CString(ip_addr)); // the CString constructor is probably unnecessary


    Reply
  • GOOD BOY

    Posted by Legacy on 12/24/2003 12:00am

    Originally posted by: Best Programmer

    YES THIS IS THE QUICKEST WAY.....YOU GET THE AWARD FOR THE QUICKY OF THE YEAR

    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • 10 Rules that Make or Break Enterprise App Development Projects In today's app-driven world, application development is a top priority. Even so, 68% of enterprise application delivery projects fail. Designing and building applications that pay for themselves and adapt to future needs is incredibly difficult. Executing one successful project is lucky, but making it a repeatable process and strategic advantage? That's where the money is. With help from our most experienced project leads and software engineers, …

  • Best-in-Class organizations execute on a strategy that supports the multi-channel nature of customer requests. These leading organizations do not just open up their service infrastructures to accommodate new channels, but also empower their teams to deliver an effective and consistent experience regardless of the channel selected by the customer. This document will highlight the key business capabilities that support a Best-in-Class customer engagement strategy.

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds