C/C++检测网络类型

发布于 2024-10-17 10:58:30 字数 795 浏览 0 评论 0原文

我需要编写一个 win32 c/c++ 应用程序,它将能够确定其运行的 PC 是否连接到 2 个网络之一。第一个网络是公司 LAN(没有互联网连接),第二个网络是一个独立交换机,连接有一台 PC(运行程序的 PC)。

我对网络编程还很陌生,但到目前为止,我已经尝试测试是否可以映射 LAN 上保存的网络驱动器。如果 PC 连接到 LAN,则此操作正常,驱动器映射成功,因此 LAN 检测成功。但是,如果 PC 连接到交换机,这会导致非常长的超时,这是不合适的,因为它会延迟程序以致使其无法使用。

有人有任何替代建议吗?

我在 VS 6.0 中使用 c/c++

[更新]

在尝试一些不同的想法并查看下面的一些建议时,我认为我应该更新一些附加信息,因为许多(如果不是全部)建议我都没有认为会起作用。

(1) 上述 LAN 完全没有外部连接,完全隔离,因此无法解析外部 DNS 或 ping 网站。

(2) 主机名、MAC 地址、IP、默认网关、子网等(基本上您在 ipconfig -all 中看到的所有内容)都是手动配置的(不是从路由器动态配置的),因此检查这些设置中的任何一个都将返回相同的值,无论是否连接到LAN 或交换机。

(3) 由于第 (2) 点,任何与交换机通信的尝试似乎都不成功,事实上几乎所有网络命令(ping、arp 等)似乎都失败 - 我认为是由于机器尝试连接到 LAN当它不存在时:-(

我发现有效的一件事是 ping 默认网关 IP,当连接到交换机时超时。这有点好,因为我可以减少 ping 的超时,所以它不仅仅是挂了很长时间,但感觉有点像黑客,我当然会感激任何更好的解决方案,

谢谢。

I need to write a win32 c/c++ application which will be able to determine whether the PC it's running on is connected to one of 2 networks. The first network is the company LAN (which has no internet connection) and the second network is a standalone switch with a single PC connected to it (the PC that the program is running on).

I'm pretty new to network programming but so far I have tried testing to see if a network drive which is held on our LAN can be mapped. This works fine if the PC is connected to the LAN, the drive mapping succeeds so so LAN detection is successful. However, if the PC is connected to the switch, this results in a VERY long timeout which is not a suitable as it will delay the program so much as to make it unusable.

Does anyone have any alternative suggestions?

I'm using c/c++ in VS 6.0

[Update]

Whilst trying a few different ideas and looking at some of the suggestions below I thought I should update with some additional information as many (if not all) of the suggestions I don't think will work.

(1) The aforementioned LAN has no external connections at all, it is completely isolated so no resolving of external DNS or pinging websites is possible.

(2) Hostname, MAC address, IP, Default Gateway, Subnet etc etc (basically everything you see in ipconfig -all) are all manually configured (not dynamic from the router) so checking any of these settings will return the same whether connected to the LAN or the switch.

(3) Due to point (2), any attempts to communicate with the switch seem to be unsuccessful, in fact almost all networking commands (ping, arp etc) seem to fail - I think due to the machine trying to connect to the LAN when it isn't there :-(

One thing I have found which works is pinging the default gateway IP which times out when connected to the switch. This is sort of ok as I can reduce the timeout of ping so it doesn't just hang for ages but it feels like a bit of a hack and I would certainly appreciate any better solutions.

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

甜尕妞 2024-10-24 10:58:30

就 TCP/IP 而言,不存在 WAN 上的 LAN 之类的东西。有一组非互联网可路由地址,如 192.168.xx 和 10.xxx,但有时 ISP 会在缺乏 IP 地址时使用这些地址。

最好的办法是在建立 TCP/IP 连接时使用异步 API。为此,WIN32 定义了一整套 OVERLAPPED API。这将防止您的应用程序在等待远程连接时陷入停顿。

或者将套接字内容放入另一个线程中,然后仅在操作完成时通知 UI。

As far as TCP/IP is concerned there is no such thing as a LAN on WAN. There are a set of non-internet routable addresses like 192.168.x.x and 10.x.x.x but these are sometimes used by ISP short of IP addresses.

You best bet is to use Asynchronous APIs when making TCP/IP connections. WIN32 defines a whole buch of OVERLAPPED APIs for this purpose. This will prevent your application from grinding to a halt while waiting for a remote connection.

Alternatively put the socket stuff into another thread and then only notify the UI when the operation is done.

空袭的梦i 2024-10-24 10:58:30

我首先尝试使用本地可用的信息(即来自您的计算机的信息)来区分两者。 ipconfig /all 的输出是否因您连接到的网络而异?如果是这样,请尽可能利用这种差异。

是否可以获取独立交换机的 MAC 地址?控制公司LAN的交换机的?这将是一个肯定的说法。除非有人克隆了 MAC 地址。

如果您尝试使用某些网络服务是否存在来确定您连接到哪个网络,您永远无法确定。例如,如果您无法映射该网络驱动器,您只知道该网络驱动器不可用。您不能肯定地说您没有连接到公司 LAN。如果您使用 ping,情况也是如此。特定机器缺乏响应仅意味着该机器没有响应。

I would first try to differentiate between the two using information available locally--that is, from your computer. Does the output of ipconfig /all differ depending on which network you're connected to? If so, exploit that difference if you can.

Is it possible to get the MAC address of the standalone switch? Of the switch that controls the company LAN? That would be a sure way to tell. Unless somebody cloned the MAC address.

If you try using the existence or non-existence of some network service to determine which network you're connected to, you can never be sure. For example, if you failed to map that network drive, all you know is that the network drive isn't available. You can't say for certain that you're not connected to the company LAN. Same is true if you use ping. Lack of response from a particular machine means only that the machine didn't respond.

几味少女 2024-10-24 10:58:30

您可以考虑各种因素来进行区分:

DNS domain name (GetComputerNameEx)
MAC address of gateway (ping it, then GetIpNetTable)
Routing table(do you have a gateway and default route on the company LAN)
WNet discovered network resources (WNetOpenEnum, WNetEnumResource)
Ability to resolve external hostnames (try a 5-10 names like www.google.com, www.microsoft.com and so on, if one resolves you should have internet)

如果测试失败,您必须确定有多少指标“足够”来确定您位于一个或另一个 LAN 上。然后不断重试,直到得到明确的结果。

http://msdn.microsoft.com/en -us/library/aa366071%28v=VS.85%29.aspx 有许多与网络相关的函数,您可以尝试使用它们来创建更多指标。

Various things you can look at for differentiation:

DNS domain name (GetComputerNameEx)
MAC address of gateway (ping it, then GetIpNetTable)
Routing table(do you have a gateway and default route on the company LAN)
WNet discovered network resources (WNetOpenEnum, WNetEnumResource)
Ability to resolve external hostnames (try a 5-10 names like www.google.com, www.microsoft.com and so on, if one resolves you should have internet)

You'll have to decide how many indicators are "enough" to decide you're on one or the other LAN though if tests fail. Then keep retrying until you have a definite result.

http://msdn.microsoft.com/en-us/library/aa366071%28v=VS.85%29.aspx has a lot of network related functions that you can experiment with to create further indicators.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文