How can I get the IP address of the system.
I want the Ip address as I can see after doing ipconfig
or /bin/ifconfig
How can I get the IP address of the system.
I want the Ip address as I can see after doing ipconfig
or /bin/ifconfig
You mean 'IP Addresses' - use GetAdapterAddresses in Win32. There is sample code there.
It's a bit convoluted because you first call the API have to see how much memory you need, and then call the same API again with the required memory block. Then you have to walk the list of returned structures in that memory block as shown in the sample. What you are eventually gong to get to is this:
The SOCKET_ADDRESS structure is used in the IP_ADAPTER_ADDRESSES structure pointed to by the AdapterAddresses parameter. On the Microsoft Windows Software Development Kit (SDK) released for Windows Vista and later, the organization of header files has changed and the SOCKET_ADDRESS structure is defined in the Ws2def.h header file which is automatically included by the Winsock2.h header file. On the Platform SDK released for Windows Server 2003 and Windows XP, the SOCKET_ADDRESS structure is declared in the Winsock2.h header file. In order to use the IP_ADAPTER_ADDRESSES structure, the Winsock2.h header file must be included before the Iphlpapi.h header file.
At that point you can call WSAAddressToString to string-ize the IP address that's held inside the SOCKET_ADDRESS
structure, whether it's IPv6 or IPv4.
If you are behind a firewall and want to know your public IP address, you could use an HTTP client library to scrape web pages like this one (there was one that just returns the IP address as text/plain, but I can't find it right now).
If you can access .NET Framework (managed C++) you can use System.Net.Dns.GetHostAddress method. See here for more: http://msdn.microsoft.com/en-us/library/system.net.dns.gethostaddresses.aspx You actually get an array of IPs since one domain name can correspond to more than one IP.
For local IP address, you can use winsock. Take a look at this example.
If you use winsock, make sure you add proper library in your project. For example, I've to add Ws2_32.lib
in VS 2008.