如何判断网络状态?

hydonlee 2003-01-15 02:48:10
网络可用还是不可用?
就象在2000中,网络不通时,分提示网络不通了。。。

我应该怎么做? 不针对主机,只对网络。允许计算机通过各种方式连接。

那,我如何判断当前的网络是断开的?还是接通的?
...全文
127 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
NowCan 2003-01-15
  • 打赏
  • 举报
回复
经常遇到需要检测计算机是否联网的情况,那么究竟怎么判断呢?也许答案是令人很不满意的,那就是没有完全无误的办法。不过,有一些函数可以作为参考。下面是一些方法介绍,我的翻译水平有限,大家就拿原文看吧。
Actually, there is no single function for determining if a machine is
connected to the Internet, and it is impossible to reliably determine what
is happening without side effects - such as automatic network connections
taking place. What you can do is reliably detect when there definitely
isn't an Internet Link: in the absence of any dial up or LAN connection the
system is definitely off line.

Some techniques include:

1. IsNetworkAlive()
If you are targeting system with IE5 or later, this is the best API call
yet it even listens for traffic on a LAN. There is a secondary function
IsDestinationReachable() which tries to resolve the hostname and ping it.
This does not work through firewalls, and overestimates speed as the max
the LAN card can support, rather than the actual point to point bandwidth.

2. RasEnumConnections()
A reliable technique for modems and direct dial up networking, but not for
situations where Internet access is via a LAN. You should dynamically load
"RasEnumConnectionA" from "RASAPI32.DLL", as LAN installations of Windows
may not include the library.

3. InternetGetConnectedState()
This Wininet /IE4 function call can distinguish between modem and LAN, but
can't handle complex LAN+autodial router situations. It is "offline state
aware". Important: handling of the offline flage changed for IE5 -it
returns TRUE for connected' even when off line, but signals the flags in
the LPDWORD parameter.

4. InternetCheckConnection()
A Winnet/IE4 function call. This is meant to determine if a URL is
reachable- in practice it is pretty unreliable and best voided.

5. NT SP4, NT5: The IP helper API can tell you which network interface to
use to connect to a supplied IP address, and what the bandwidth and current
status of that link is

6. Using the Offline flag which is part of IE4 to allow users to manually
control the online/offline state of applications. This flag is stored in
the registry and can be manipulated via some funcions calls

These calls mostly determine the presence or absence of network connections
-not Internet access, so can't handle a home network sharing a dial up
connection, or two laptops connected directly to each other.

The global offline state flag of IE4 (and hence win98, NT5) and the call to
test it - InternetGetConnectedState()- look the best long term options, but
will take time to become universal. The IP Helper APIs even let you find
out how much traffic is going over a link, but only detect the 'loopback'
interface on Windows 98, so is not a lot of use. Wouldn't a
'GetSpeedToHost() function call be great?

Finally, whatever technique you use, when it's time to talk to a remote
site, always add timeouts or a cancel button. Even a quick functions like
gethostbyname() can lock up an app if something in the network chain is
broken.

Best regards,
yhhuang
VS.NET, Visual C++
Microsoft

18,356

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 网络编程
c++c语言开发语言 技术论坛(原bbs)
社区管理员
  • 网络编程
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧