Windows C 中如何获取 native IP 地址?我无法在以下代码中获取我机器的 IP 地址:
#include <ifaddrs.h>
#include <stdio.h>
int main()
{
struct ifaddrs *id;
int val;
val = getifaddrs(&id);
printf("Network Interface Name :- %s\n",id->ifa_name);
printf("Network Address of %s :- %d\n",id->ifa_name,id->ifa_addr);
printf("Network Data :- %d \n",id->ifa_data);
printf("Socket Data : -%c\n",id->ifa_addr->sa_data);
return 0;
}
编译时遇到错误:
fatal error C1083: Cannot open include file: 'net/if.h': No such file or directory.
我不能使用 #include <net/if.h>
因为它仅在 Linux 上可用。
最佳答案
您显示的代码无法编译,因为它是为 Linux 而不是 Windows 设计的。没有 <net/if.h>
Windows 上的 header 。
getifaddrs()
返回本地接口(interface)地址的链表。但是getifaddrs()
Windows 上根本不存在(除非您找到自定义的第 3 方实现)。
Windows 上的等效项是使用 Win32 GetAdaptersInfo()
XP 及更早版本的功能,或 GetAdaptersAddresses()
在 Vista 及更高版本上运行。这两个函数都返回详细适配器信息的链接列表(不仅仅是地址)。
C 示例在 MSDN 上的文档中提供。
关于c - 在C中查找机器的IP地址?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41139561/