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 KbDownload source - 28.0 Kb

Comments
Linux - Version ?
Posted by Urlaub in Ungarn on 11/03/2012 12:29pmHi is there a version running on linux avaible?
Replyquestion regarding modification to above code
Posted by Legacy on 12/16/2003 12:00amOriginally posted by: Avi
ReplyGetIpAddress
Posted by Legacy on 05/18/2003 12:00amOriginally posted by: tom cruz
ReplyWorks with Windows XP and 2000
Posted by Legacy on 07/19/2002 12:00amOriginally 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
ReplyDoesn't Work On W2K SERVER....
Posted by Legacy on 06/03/2002 12:00amOriginally posted by: Phan Tien Vu
ReplyDoesn't Quite Work In W2K or XP
Posted by Legacy on 05/06/2002 12:00amOriginally 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
ReplyMAC
Posted by Legacy on 03/26/2002 12:00amOriginally posted by: Jeff
How can you get the MAC address for each IP?
ReplyNot C#
Posted by Legacy on 03/25/2002 12:00amOriginally 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.
ReplyGreat
Posted by Legacy on 03/23/2002 12:00amOriginally posted by: barry
ya ! that was really great & good.
Reply