Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/windows/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何获取系统的Ip地址_C++_Windows - Fatal编程技术网

C++ 如何获取系统的Ip地址

C++ 如何获取系统的Ip地址,c++,windows,C++,Windows,如何获取系统的IP地址 我想要执行ipconfig或/bin/ifconfig后可以看到的Ip地址。您的意思是“Ip地址”-在Win32中使用。这里有示例代码 这有点复杂,因为首先调用API必须查看需要多少内存,然后使用所需的内存块再次调用相同的API。然后,您必须遍历该内存块中返回的结构列表,如示例所示。你最终要做的是: 使用SOCKET\u地址结构 在IP_适配器_地址结构中 由适配器地址指向 参数在Microsoft Windows上 软件开发工具包(SDK) 针对Windows Vist

如何获取系统的IP地址


我想要执行
ipconfig
/bin/ifconfig
后可以看到的Ip地址。您的意思是“Ip地址”-在Win32中使用。这里有示例代码

这有点复杂,因为首先调用API必须查看需要多少内存,然后使用所需的内存块再次调用相同的API。然后,您必须遍历该内存块中返回的结构列表,如示例所示。你最终要做的是:

使用SOCKET\u地址结构 在IP_适配器_地址结构中 由适配器地址指向 参数在Microsoft Windows上 软件开发工具包(SDK) 针对Windows Vista及更高版本发布, 头文件的组织已更改 已更改和套接字地址 结构在Ws2def.h中定义 自动删除的头文件 包含在Winsock2.h标头中 文件在为发布SDK的平台上 Windows Server 2003和Windows XP, 套接字地址结构为 在Winsock2.h标头中声明 文件为了使用 IP_适配器_地址结构 Winsock2.h头文件必须是 包含在iphlapi.h头之前 文件


此时,您可以调用字符串来对存储在
套接字\u地址结构中的IP地址进行字符串化,无论是IPv6还是IPv4。

您的意思是“IP地址”-在Win32中使用。这里有示例代码

这有点复杂,因为首先调用API必须查看需要多少内存,然后使用所需的内存块再次调用相同的API。然后,您必须遍历该内存块中返回的结构列表,如示例所示。你最终要做的是:

使用SOCKET\u地址结构 在IP_适配器_地址结构中 由适配器地址指向 参数在Microsoft Windows上 软件开发工具包(SDK) 针对Windows Vista及更高版本发布, 头文件的组织已更改 已更改和套接字地址 结构在Ws2def.h中定义 自动删除的头文件 包含在Winsock2.h标头中 文件在为发布SDK的平台上 Windows Server 2003和Windows XP, 套接字地址结构为 在Winsock2.h标头中声明 文件为了使用 IP_适配器_地址结构 Winsock2.h头文件必须是 包含在iphlapi.h头之前 文件


此时,您可以调用字符串来指定存放在
套接字\u地址
结构中的IP地址,无论是IPv6还是IPv4。

您的问题不是很具体,但这应该会有帮助:


您的问题不是很具体,但这应该会有帮助:


如果你在防火墙后面,想知道你的公共IP地址,你可以使用HTTP客户端库来抓取网页,比如(有一个只返回文本/普通的IP地址,但我现在找不到)。

如果你在防火墙后面,想知道你的公共IP地址,您可以使用HTTP客户端库来抓取网页,如(有一个只以文本/普通形式返回IP地址,但我现在找不到它)。

如果您可以访问.NET Framework(托管C++),则可以使用System.NET.Dns.GetHostAddress方法。请参见此处了解更多信息:
实际上,由于一个域名可以对应多个IP,因此您可以获得一个IP数组。

如果您可以访问.NET Framework(托管C++),则可以使用System.NET.Dns.GetHostAddress方法。请参见此处了解更多信息:
实际上,由于一个域名可以对应多个IP,因此您可以获得一个IP数组。

对于本地IP地址,您可以使用winsock。看一看


如果使用winsock,请确保在项目中添加适当的库。例如,我必须在VS 2008中添加
Ws2_32.lib

对于本地IP地址,可以使用winsock。看一看

// 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;
}
如果使用winsock,请确保在项目中添加适当的库。例如,我必须在VS 2008中添加
Ws2_32.lib

//要求调用WSAStartup
// 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;
}
std::vector getipaddress(const std::string和hostname) { std::向量结果; //我们使用getaddrinfo(gethostbyname已被弃用) 结构addrinfo提示={0}; hits.ai_family=AF_unsec;//同时需要IPv4和IPv6 hits.ai_socktype=SOCK_流; hits.ai_protocol=IPPROTO_TCP; 结构addrinfo*paddrinfo=NULL; if(getaddrinfo(hostname.c_str()、NULL、提示和paddrinfo)!=0) 返回结果;//出现问题,返回空列表 //计算机可以有多个IP地址(IPv4、IPv6等) for(struct addrinfo*ptr=paddrinfo;ptr!=NULL;ptr=ptr->ai_next) { //inet_ntop不适用于所有版本的Windows,我们实现自己的 字符地址[NI_MAXHOST]={0}; 如果(ptr->ai_系列==AF_INET) { 如果(getnameinfo(ptr->ai_addr,sizeof(struct sockaddr_in),ipaddress,_countof(ipaddress)-1,NULL,0,NI_numericost)==0) result.push_back(std::string(ipaddress)); } 否则如果(ptr->ai_系列==AF_INET6) { if(getnameinfo(ptr->ai_addr,sizeof(struct sockaddr_in6),ipaddress,_countof(ipaddress)-1,NULL,0,NI_numericost)==0) result.push_back(std::string(ipaddress)); } } freeaddrinfo(paddrinfo); 返回结果; }
//要求已调用WSAStartup
std::vector getipaddress(const std::string和hostname)
{
std::向量结果;
//我们使用getaddrinfo(gethostbyname已被弃用)
结构addrinfo提示={0};
hits.ai_family=AF_unsec;//同时需要IPv4和IPv6
hits.ai_socktype=SOCK_流;
hits.ai_protocol=IPPROTO_TCP;
结构addrinfo*paddrinfo=NULL;
if(getaddrinfo(hostname.c_str()、NULL、提示和paddrin