你如何找出哪个网卡连接到互联网? [英] How do you find out which NIC is connected to the internet?

查看:80
本文介绍了你如何找出哪个网卡连接到互联网?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑以下设置:带有 LAN 接口和 WiFi 接口的 Windows PC(任何新笔记本电脑的标准配置).每个接口都可能与网络连接或断开连接.我需要一种方法来确定哪个适配器是连接到 Internet 的适配器 - 特别是,如果它们都连接到不同的网络,一个连接到 Internet,另一个没有连接.

Consider the following setup: A windows PC with a LAN interface and a WiFi interface (the standard for any new laptop). Each of the interfaces might be connected or disconnected from a network. I need a way to determine which one of the adapters is the one connected to the internet - specifically, in case they are both connected to different networks, one with connection to the internet and one without.

我目前的解决方案涉及使用 IPHelper 的GetBestInterface" 函数并为其提供 IP 地址0.0.0.0".

My current solution involves using IPHelper's "GetBestInterface" function and supplying it with the IP address "0.0.0.0".

对于这个问题,您有其他的解决方案吗?

Do you have any other solutions you might suggest to this problem?

根据一些答案,让我详细说明:

Following some of the answers, let me elaborate:

  • 我需要这个,因为我有一个产品必须选择要绑定到哪个适配器.我无法控制网络的设置或产品将运行的主机,因此我需要一个尽可能健壮的解决方案,尽可能少的假设.
  • 我需要在代码中执行此操作,因为这是产品的一部分.

@Chris Upchurch:这让我依赖于 google.com 的启动(通常不是问题)以及可能安装的任何个人防火墙以允许 ping.

@Chris Upchurch: This makes me dependent on google.com being up (usually not a problem) and on any personal firewall that might be installed to allow pinging.

@Till:就像 Steve Moon 所说的那样,依赖适配器的地址是有风险的,因为您对内部网络设置做了很多假设.

@Till: Like Steve Moon said, relying on the adapter's address is kind of risky because you make a lot of assumptions on the internal network setup.

@Steve Moon:查看路由表听起来是个好主意,但我没有自己应用路由逻辑,而是尝试使用如上所述的GetBestInterface".我相信它应该做的正是您在回答中概述的内容,但我不确定.我不愿意实现自己的路由逻辑"的原因是,与使用由更多核心"网络人员编写和测试的库/API 相比,我更容易出错.

@Steve Moon: Looking at the routing table sounds like a good idea, but instead of applying the routing logic myself, I am trying to use "GetBestInterface" as described above. I believe what it should do is exactly what you outlined in your answer, but I am not really sure. The reason I'm reluctant to implement my own "routing logic" is that there's a better chance that I'll get it wrong than if I use a library/API written and tested by more "hard-core" network people.

推荐答案

从技术上讲,没有连接到 Internet".真正的问题是,哪个接口可以路由到所需地址.现在,您正在查询默认路线" - 如果不存在通往目的地的特定路线,则该路线适用.但是,您忽略了任何特定路线.

Technically, there is no "connected to the Internet". The real question is, which interface is routeable to a desired address. Right now, you're querying for the "default route" - the one that applies if no specific route to destination exists. But, you're ignoring any specific routes.

幸运的是,对于 99.9% 的家庭用户来说,这可以解决问题.他们不太可能有太多的路由表,GetBestInterface 会自动选择有线而不是无线 - 所以你应该很好.为您搞砸的 0.1% 的案例添加一个覆盖选项,然后收工.

Fortunately, for 99.9% of home users, that'll do the trick. They're not likely to have much of a routing table, and GetBestInterface will automatically prefer wired over wireless - so you should be good. Throw in an override option for the .1% of cases you screw up, and call it a day.

但是,对于公司用途,您应该针对特定目的地使用 GetBestInterface - 否则,如果有人与您的目的地在同一 LAN 上,您就会遇到问题(这意味着您应该采用内部"接口,而不是外部")或具有到目的地的特定路线(例如,我的内部网络可以与目的地的网络对等).

But, for corporate use, you should be using GetBestInterface for a specific destination - otherwise, you'll have issues if someone is on the same LAN as your destination (which means you should take the "internal" interface, not the "external") or has a specific route to your destination (my internal network could peer with your destination's network, for instance).

再说一次,我不确定你打算用这个连接到互联网"的适配器做什么,所以这可能没什么大不了的.

Then again, I'm not sure what you plan to do with this adapter "connected to the Internet", so it might not be a big deal.

这篇关于你如何找出哪个网卡连接到互联网?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆