C++获取本地IP地址,区分VPN和本地网络
我的计算机连接到 IP 地址为 10.3.3.3 的本地网络(以太网适配器),并连接到 IP 地址为 172.4.0.70 的 VPN(PPP 适配器)
现在,我如何获取我的本地 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;
}
由 小码哥发布于 VS2008 Win7 64位
My computer is connected to the local network(ethernet adapter) with ip address 10.3.3.3 and it is connected with my VPN(PPP adapter) with the IP address 172.4.0.70
Now, how can I get my local IP(10.3.3.3) programmatically?
I had tested using the following code.
But I can't differentiate between the VPN and local network, any help will be much appreciated.
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 64bits
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
几年后...
刚刚遇到了同样的问题,并找到了解决方案。您可以使用 GetAdaptersInfo() 获取所有本地适配器,然后循环浏览列表并选择第一个具有有效子网掩码和默认网关的适配器(我的 VPN 适配器没有)。
如果您同时拥有带有默认网关的 WiFi 和以太网!!,我想您可以使用 Type 字段来选择以太网。
这类似于 MSDN 上的 GetAdaptersInfo() 示例代码。
Few years later...
Just ran into the same issue, and figured out a solution. You can use GetAdaptersInfo() to get all the local adapters, and then cycle through the list and pick the first with a valid subnet mask and a default gateway (my VPN adapter didn't have one).
If you have both WiFi and Ethernet with default gateways!!, I guess you can use the Type field to pick Ethernet..
This is similar to the sample code for GetAdaptersInfo() on MSDN.