Obtain all IP addresses of local machine



Click here for larger image

Environment: Compiled on: Visual Studio .NET & Windows XP Pro. Code works on any Windows machine.

You're obviously writing TCP/IP applications. You have the ability to create sockets and bind them to specific ports. You may bind to all the ports, you may bind to 127.0.0.1, or finally you may want to bind to specific IP addresses of your local machine.

If you do NOT wish to bind to all ports because then you can't have other applications serving off the same machine. You may want bind to a specific IP address of your local machine, then you'd need to find out how to obtain all the IP addresses located on your machine.

Attached is the code required to simply start up winsock in a Windows environment, and enumerate all the IP addresses on your local machine. It is a console application, no MFC, not much Windows specific code.

It has been tested in a Windows XP Professional environment, along with a Windows 2000 Server environment. We enumarate all the IPs until the list terminates (with a NULL pointer). It should have no problems working with Windows 95/98/Me/2k/XP/CE.

I also tested it in our server environment where we have 32 IP addresses and we have software listening on the same port on different IPs, all located on the one NIC card that has access to the internet. In fact we are hosting listening servers on port 80 along side IIS. This requires you to disable socket pooling, but that is another story.

If you have any questions, don't hesitate to e-mail me.

#include <stdio.h>
#include <WinSock.h>
#pragma comment(lib, "wsock32.lib")

int main(int argc, char *argv[])
{
 WORD wVersionRequested;
 WSADATA wsaData;
 char name[255];
 PHOSTENT hostinfo;
 wVersionRequested = MAKEWORD( 1, 1 );
 char *ip;

 if ( WSAStartup( wVersionRequested, &wsaData ) == 0 )
  if( gethostname ( name, sizeof(name)) == 0)
  {
   printf("Host name: %s\n", name);

   if((hostinfo = gethostbyname(name)) != NULL)
   {
    int nCount = 0;
    while(hostinfo->h_addr_list[nCount])
    {
     ip = inet_ntoa(*(
          struct in_addr *)hostinfo->h_addr_list[nCount]);

     printf("IP #%d: %s\n", ++nCount, ip);
    }
   }
  }
 return 0;
}

Downloads

Download demo project - 28.0 Kb
Download source - 28.0 Kb


Comments

  • Linux - Version ?

    Posted by Urlaub in Ungarn on 11/03/2012 12:29pm

    Hi is there a version running on linux avaible?

    Reply
  • question regarding modification to above code

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

    Originally posted by: Avi

    Hi!
    
    I used this program and it gives me correct results. Thanks for it. I have a suggestion there should be WSAcleanup() at end of your program before return.
    I modified this code little bit and tried to use the command which I know, but i don't get the correct ip address
    int main(int argc, char *argv[])
    {

    struct sockaddr_in name;
    char hostname[255];
    struct hostent *phe;
    char* ipaddr;
    WORD wVersionRequested;
    WSADATA wsaData;
    wVersionRequested = MAKEWORD( 1, 1 );
    if(WSAStartup(wVersionRequested, &wsaData) != 0){
    printf("\nUnable to initialize Winsock DLL.");
    return -1;

    }
    memset((char*)&name, 0, sizeof(name));
    if(gethostname(hostname, 255) < 0)
    {
    printf("\nError in obtaining host name");
    exit(0);
    }

    phe = gethostbyname(hostname);
    if(phe == NULL){
    printf("\nError in gethostbyname");
    exit(0);
    }

    //bcopy(phe->h_addr_list, &name.sin_addr, phe->h_length);
    memcpy(&name.sin_addr, phe->h_addr_list, phe->h_length);

    ipaddr = inet_ntoa(name.sin_addr);

    printf("\nIPAddress of host = %s", ipaddr);
    //printf("\nIPAddress = %d", name.sin_addr.S_un);
    WSACleanup();
    return -1;
    }dress. Can any one tell where I am going wrong?

    Reply
  • GetIpAddress

    Posted by Legacy on 05/18/2003 12:00am

    Originally posted by: tom cruz

    Thank you very much.  This is just what I was looking for.
    
    

    tomcruz.net

    void __fastcall TForm1::Button2Click(TObject *Sender)
    {

    WORD wVersionRequested;
    WSADATA wsaData;
    char name[255];
    String ip;
    PHOSTENT hostinfo;
    wVersionRequested = MAKEWORD( 2, 0 );

    if ( WSAStartup( wVersionRequested, &wsaData ) == 0 )
    {
    if( gethostname ( name, sizeof(name)) == 0)
    {
    Edit1->Text = name;
    if((hostinfo = gethostbyname(name)) != NULL)
    Edit2->Text = inet_ntoa (*(struct in_addr *)*hostinfo->h_addr_list);
    }

    WSACleanup( );
    }

    }

    void __fastcall TForm1::Button1Click(TObject *Sender)
    {
    // load a list box with the ip addresses;
    WORD wVersionRequested;
    WSADATA wsaData;
    char name[255];
    PHOSTENT hostinfo;
    wVersionRequested = MAKEWORD( 1, 1 );
    char *ip;

    if ( WSAStartup( wVersionRequested, &wsaData ) == 0 )
    if( gethostname ( name, sizeof(name)) == 0)
    {
    if((hostinfo = gethostbyname(name)) != NULL)
    {
    int nCount = 0;
    while(hostinfo->h_addr_list[nCount])
    {
    ListBox1->Items->Add ( inet_ntoa(*
    (struct in_addr *)hostinfo->h_addr_list[nCount]));

    nCount++;
    }
    }
    }
    }

    Reply
  • Works with Windows XP and 2000

    Posted by Legacy on 07/19/2002 12:00am

    Originally posted by: Vikram Jairam

    It enumerates all the active IP Addresses on your computer just as the author promises. Really useful if you want to write a daemon that manages port-IP combinations on the same machine. Helped a lot. Thanks Khaled

    Reply
  • Doesn't Work On W2K SERVER....

    Posted by Legacy on 06/03/2002 12:00am

    Originally posted by: Phan Tien Vu

    Hi... I complied and run the program. But it doesn't work on W2K SERVER. It only get IP Address on the Server, all the local machine (WINXP, WINME,...) is not. Why ??? Please tell me how to change the code...
    
    How do I get all the subnet mask ??? Please tell me...
    Thanks,

    Reply
  • Doesn't Quite Work In W2K or XP

    Posted by Legacy on 05/06/2002 12:00am

    Originally posted by: Shaun Staley

    It works ok when the computer is connected to the network. However, as soon as you unplug the cable from the network, XP & 2000 says the cable is unplugged. When that happens, it gives you an IP address of 127.0.0.1. Is there any fix for this? Thanks

    Reply
  • MAC

    Posted by Legacy on 03/26/2002 12:00am

    Originally posted by: Jeff

    How can you get the MAC address for each IP?

    Reply
  • Not C#

    Posted by Legacy on 03/25/2002 12:00am

    Originally posted by: Khalid Shaikh

    Small problem guys,

    This isn't C#.

    :(

    It is C++. I can look into C# if everyone is so excited about it.

    11:02AM PST, Monday March 25

    Okay guys, I just wrote up the C# version. It is kind of cool having a side by side C++ implementation as well! :P

    Going to submit it.

    Reply
  • Great

    Posted by Legacy on 03/23/2002 12:00am

    Originally posted by: barry

    ya ! that was really great & good.

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

Top White Papers and Webcasts

  • Live Event Date: May 6, 2014 @ 1:00 p.m. ET / 10:00 a.m. PT While you likely have very good reasons for remaining on WinXP after end of support -- an estimated 20-30% of worldwide devices still are -- the bottom line is your security risk is now significant. In the absence of security patches, attackers will certainly turn their attention to this new opportunity. Join Lumension Vice President Paul Zimski in this one-hour webcast to discuss risk and, more importantly, 5 pragmatic risk mitigation techniques …

  • Download the Information Governance Survey Benchmark Report to gain insights that can help you further establish business value in your Records and Information Management (RIM) program and across your entire organization. Discover how your peers in the industry are dealing with this evolving information lifecycle management environment and uncover key insights such as: 87% of organizations surveyed have a RIM program in place 8% measure compliance 64% cannot get employees to "let go" of information for …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds