检测到机器是否已连接/可用?
如何检测机器在当前网络中是否已连接/可用。
当然,它有多种用途,但我主要担心的是,我的应用程序使用位于特定计算机中的资源,如果它们不可用,它甚至不会尝试连接,而是使用本地资源。
How can I detect if a machine is connected/available in the present network.
It has several uses of course, but my main concern here is that my application uses resources located in specific machines and if they are not available it will not even attempt the connection and will use local resources.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
与尝试连接不同的策略:让服务器通过中间件(管道?)发送 UDP 广播或某种心跳信号来告诉客户端服务是否仍然可用,客户端会监听这些信号 - 发布/订阅通信模型。
A different strategy than trying to connect: let the server tell the clients if the services are still available, by sending UDP Broadcast or some kind of heartbeat signal over middleware (pipes?), which the clients listens to - a publish/subscribe communication model.
你可以尝试对机器进行 ping 操作。查看这篇文章
使用 Delphi 进行 PING和 WMI
。you can try making a ping to the machine. check this article
Making a PING with Delphi and the WMI
.ICMP 回显请求 (PING) 将告诉您计算机是否已启动并且可以通过网络访问。它不会告诉您要连接的服务在计算机上是否可用(已启动并正在运行)。
最好的选择可能是只尝试连接,如果连接失败,则回退到本地资源。
ICMP echo request (PING) will tell you if the machine is up and reachable on the network. It will not tell you if the service you want to connect to is available on the machine (up and running).
Best bet would probably be to just attempt the connection and fall back to local resources if the connection fails.
只需尝试使用该资源,如果出现错误,请改用本地资源。您尝试实现的策略存在几个问题,包括测试和使用之间的时间窗口,在此期间资源可能变得不可用,并且实际上并没有测试资源的可用性,仅测试某些资源的可用性低阶事物,例如 TCP 端口或堆栈的 ICMP 回显部分。一般来说,检测资源是否可用的最佳方法就是尝试使用它并从故障中恢复。无论如何,您都必须编写代码来处理这些故障,为什么要这样做两次呢?
Just try to use the resource and if you get an error use the local resource instead. The strategy you are trying to implement suffers from several problems including timing windows between the test and the use, during which the resource may become unavailable, and also doesn't actually test the resource for availability, only some lower-order thing like a TCP port or the ICMP echo part of the stack. In general the best way to detect whether a resource is available is just to try to use it, and recover from the failures. You have to write code to handle those failures anyway, why do it all twice?