【发布时间】:2014-03-15 16:41:19
【问题描述】:
我的电脑连接到本地网络(以太网适配器),IP 地址为 10.3.3.3,它连接到我的 VPN(PPP 适配器),IP 地址为 172.4.0.70
现在,如何以编程方式获取本地 IP(10.3.3.3)?
我使用以下代码进行了测试。
但我无法区分 VPN 和本地网络,非常感谢任何帮助。
WSAData .....;
char* address=NULL;
getLocalIP(&address);
int getLocalIP(char** raddr)
{
char ac[80];
if (gethostname(ac, sizeof(ac)) == SOCKET_ERROR)
{
return 1;
}
struct hostent *phe = gethostbyname(ac);
if (phe == 0)
{
return 1;
}
for (int i = 0; phe->h_addr_list[i] != 0; ++i)
{
struct in_addr addr;
memcpy(&addr, phe->h_addr_list[i], sizeof(struct in_addr));
//How can I tell if it's not VPN?
//if (isnotVPN){
*raddr=inet_ntoa(addr); //<== ip address
//break;}
}
return 0;
}
c++ VS2008 Win7 64位
【问题讨论】:
-
你在 phe->h_addr_list[i] 上循环,但是如果它 > 1,你只会得到最后一个地址。
-
你想用它做什么?基本上,是否是 VPN 是应用程序不应该知道或关心的链路层的实现细节。
-
@Peter:不,它不是重复的。这个问题要求在其他问题之上提供一些额外信息。
-
您可能可以从 IP Helper 函数之一获取此信息,例如 GetAdaptersInfo()。