开发者

How Can I get the Ip address of System

How can I get the IP address of the system.

I want the Ip address as I can see after doing ipconfig or /b开发者_如何学运维in/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.


Your question isn't very specific, but this should help:

http://www.codeguru.com/forum/showthread.php?t=233261


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.


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).


// Requires that WSAStartup has been called
std::vector<std::string> GetIPAddresses(const std::string& hostname)
{
    std::vector<std::string> result;

    // We use getaddrinfo (gethostbyname has been deprecated)
    struct addrinfo hints = {0};
    hints.ai_family = AF_UNSPEC;    // Want both IPv4 and IPv6
    hints.ai_socktype = SOCK_STREAM;
    hints.ai_protocol = IPPROTO_TCP;

    struct addrinfo *paddrinfo = NULL;

    if (getaddrinfo(hostname.c_str(), NULL, &hints, &paddrinfo)!=0)
        return result;  // Something is wrong, empty list returned

    // The machine can have multiple IP addresses (IPv4, IPv6, etc.)
    for(struct addrinfo *ptr=paddrinfo; ptr != NULL ;ptr=ptr->ai_next)
    {
        // inet_ntop is not available for all versions of Windows, we implement our own
        char ipaddress[NI_MAXHOST] = {0};
        if (ptr->ai_family == AF_INET)
        {
            if (getnameinfo(ptr->ai_addr, sizeof(struct sockaddr_in), ipaddress, _countof(ipaddress)-1, NULL, 0, NI_NUMERICHOST)==0)
                result.push_back(std::string(ipaddress));
        }
        else if (ptr->ai_family == AF_INET6)
        {
            if (getnameinfo(ptr->ai_addr, sizeof(struct sockaddr_in6), ipaddress, _countof(ipaddress)-1, NULL, 0, NI_NUMERICHOST)==0)
                result.push_back(std::string(ipaddress));
        }
    }

    freeaddrinfo(paddrinfo);

    return result;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜