简体   繁体   English

如何确定另一台本地计算机是否还活着?

[英]How can I determine if another local machine is alive?

Is it possible to make my local computer function as a gateway in Java? 是否可以使我的本地计算机在Java中充当网关? I need the other local machines to connect directly to my computer to see if they are alive or not. 我需要其他本地机器直接连接到我的电脑,看看它们是否还活着。

You could run a Java server program on your desired PC and let it listen on a port. 您可以在所需的PC上运行Java服务器程序,让它在端口上侦听。 Then you could use other programs (browser, other Java programs etc.) to connect to this port, and send commands to be executed by the Java server program. 然后,您可以使用其他程序(浏览器,其他Java程序等)连接到此端口,并发送要由Java服务器程序执行的命令。
If you just want to see if the PC is turned on or not, I'd just use the ping command though. 如果你只是想看看PC是否打开,我只是使用ping命令。 Or see this answer: How to do a true Java ping from Windows? 或者看到这个答案: 如何从Windows进行真正的Java ping?

Try this. 试试这个。

  1. Create a Java Server Socket, which keeps listening to the client at some port. 创建一个Java Server Socket,它在某个端口继续监听客户端。

  2. Write a client in Java which connects to the Server, wrap the connection logic in try-catch block.... 用Java编写一个连接到Server的客户端,在try-catch块中包装连接逻辑....

  3. If your host is alive the try code is executed which contains the code to connect to the Server, if this connection process fails you will get UnknownHostException, here you can instead type a message that the connection failed. 如果您的主机处于活动状态,则会执行包含连接到服务器的代码的try代码,如果此连接过程失败,您将获得UnknownHostException,此处您可以输入连接失败的消息。

Surely it's the other way round? 当然这是另一回事? Surely you want to connect to the other machines to see if they're alive? 当然你想连接到其他机器,看看它们是否还活着? In which case see InetAddress.isReachable() . 在这种情况下,请参阅InetAddress.isReachable()

You could more easily manage and control this by polling for other devices from a central server. 您可以通过从中央服务器轮询其他设备来更轻松地管理和控制此操作。 If possible, avoid unnecessary client/agent apps that might tax your development and support resources as well as taking up RAM on the client workstations. 如果可能,请避免不必要的客户端/代理应用程序,这些应用程序可能会对您的开发和支持资源造成负担,并在客户端工作站上占用RAM。

There are many monitoring tools that already do what you want. 有许多监控工具已经可以满足您的需求。 I'd have a look at Nagios , for example. 例如,我会看看Nagios

If you want to develop your own app, do your own quick troubleshooting, or just get a feel for network discovery tools, then take a look at NMAP . 如果您想开发自己的应用程序,进行自己的快速故障排除,或者只是感受网络发现工具,那么请查看NMAP You could, for example, search a subnet for anything that responds to TCP:445 and see what Windows machines are alive. 例如,您可以在子网中搜索响应TCP:445的任何内容,并查看哪些Windows计算机处于活动状态。

If you do go the Nmap route, please have a look at Nmap4j on Sourceforge. 如果您确实使用了Nmap路线,请查看Sourceforge上的Nmap4j。 It's a Java wrapper API that simplifies the work needed to integrate Java and Nmap. 它是一个Java包装器API,简化了集成Java和Nmap所需的工作。

Cheers! 干杯!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM