如何判断 OS X 上是否有活动的网络连接?

发布于 2024-08-03 21:54:49 字数 246 浏览 3 评论 0原文

我想在网络请求失败时向用户提供一些有意义的错误消息。

在 Windows 上,我可以调用 InternetGetConnectedState() 来查看是否有活动的网络连接。 OS X 的等效项是什么?

如果示例代码很复杂,则可以获得奖励积分。

I want to provide my user with some meaningful error messages when network requests fail.

On Windows I can call InternetGetConnectedState() to see if there is an active network connection. What is the OS X equivalent?

Bonus points for example code if it's complex.

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

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

发布评论

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

评论(4

策马西风 2024-08-10 21:54:49

不要寻找活动的网络连接,寻找您的目标主机 可达。它并不复杂,所以没有示例代码:-)

更新:我想我应该解释一下在此上下文中可达的含义。如文档中所述,如果发往远程网络地址的数据包可以通过任何网络接口离开此计算机,则该地址被认为是可达的。粗略地说,这表明 i/f 已配置并且能够直接传送到远程系统,或者有可用的路由到达远程系统。可达性并不描述与远程主机是否“启动”或该计算机与远程主机之间的网络是否正常运行有关的任何信息。

Don't look for an active network connection, look for your target host being reachable. It's not complex, so no example code :-)

Update: I think I should have explained what reachable means in this context. As described in the docs, a remote network address is considered reachable if a packet destined for that address could leave this computer via any network interface. This is, roughly speaking, an expression of the fact that the i/f is configured and able to either directly deliver to the remote system, or has a route available to get to it. Reachability doesn't describe anything relating to whether the remote host is 'up' or whether the network between this computer and the remote one is functioning properly.

梦与时光遇 2024-08-10 21:54:49

OSX 积极支持网络/连接情况的变化,并在发生变化时随时重新配置。因此,它希望应用程序开发人员不要假设网络将始终保持不变。您可能会发现 系统配置目标和架构 一本有趣的读物,以及检查 configd 的工作原理。 (这只是从阅读中得到的,我从来没有在Mac上真正编写过这样的东西)

OSX actively supports changes in the network/connection situation, and reconfigures any time there is a change. As a consequence it expects application developers to not assume the network will always stay the same. You might find System Configuration Goals and Architecture an interesting read, as well as checking up on the workings of configd. (This is just from reading about it, I've never actually programmed anything like this on the mac)

梦魇绽荼蘼 2024-08-10 21:54:49

SystemConfiguration 框架还通过一个简单的命令行实用程序 scutil(8) 公开:

$ scutil -r stackoverflow.com
Reachable

The SystemConfiguration framework is also exposed through a simple command line utility, scutil(8):

$ scutil -r stackoverflow.com
Reachable
折戟 2024-08-10 21:54:49

如果运行 ifconfig 命令行工具,您将得到如下输出:

en0: flags=8863<UP,BROADCAST,SMART,RUNNING,SIMPLEX,MULTICAST> mtu 1500
        ether 00:0d:93:48:d8:f2 
        media: autoselect (none) status: inactive
        supported media: none autoselect 10baseT/UTP <half-duplex> 10baseT/UTP <full-duplex> 10baseT/UTP <full-duplex,hw-loopback> 100baseTX <half-duplex> 100baseTX <full-duplex> 100baseTX <full-duplex,hw-loopback> 1000baseT <full-duplex> 1000baseT <full-duplex,hw-loopback> 1000baseT <full-duplex,flow-control> 1000baseT <full-duplex,flow-control,hw-loopback>
en1: flags=8863<UP,BROADCAST,SMART,RUNNING,SIMPLEX,MULTICAST> mtu 1500
        inet6 fe80::211:24ff:fe27:385f%en1 prefixlen 64 scopeid 0x5 
        inet 192.168.1.67 netmask 0xffffff00 broadcast 192.168.1.255
        ether 00:11:24:27:38:5f 
        media: autoselect status: active
        supported media: autoselect

在我的例子中,请注意我的 en0 未连接,而我的 en1 已连接。相应地,有“状态:活动”或“状态:非活动”。您可以运行 ifconfig,解析输出,并查看是否有任何活动接口。除了环回之外。

可能还有更多 API 式的方法可以做到这一点,但我不能告诉你它们是什么!

If you run the ifconfig command-line tool, you get output like this:

en0: flags=8863<UP,BROADCAST,SMART,RUNNING,SIMPLEX,MULTICAST> mtu 1500
        ether 00:0d:93:48:d8:f2 
        media: autoselect (none) status: inactive
        supported media: none autoselect 10baseT/UTP <half-duplex> 10baseT/UTP <full-duplex> 10baseT/UTP <full-duplex,hw-loopback> 100baseTX <half-duplex> 100baseTX <full-duplex> 100baseTX <full-duplex,hw-loopback> 1000baseT <full-duplex> 1000baseT <full-duplex,hw-loopback> 1000baseT <full-duplex,flow-control> 1000baseT <full-duplex,flow-control,hw-loopback>
en1: flags=8863<UP,BROADCAST,SMART,RUNNING,SIMPLEX,MULTICAST> mtu 1500
        inet6 fe80::211:24ff:fe27:385f%en1 prefixlen 64 scopeid 0x5 
        inet 192.168.1.67 netmask 0xffffff00 broadcast 192.168.1.255
        ether 00:11:24:27:38:5f 
        media: autoselect status: active
        supported media: autoselect

In my case, note that my en0 is not connected, and my en1 is. Correspondingly, there is either 'status:active' or 'status: inactive'. You could run ifconfig, parse the output, and see if there are any active interfaces. Apart from loopback.

There are probably more API-ish ways to do this, but i can't tell you what they are!

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