简体   繁体   English

Java网络

[英]networking in java

I want to check whether a machine is in the network or not using ip address, and I used 我想检查一台机器是否在网络中,或者不使用IP地址,并且我使用了

if (InetAddress.getByName(ip).isReachable(3000))

but by using this my complete project has become very slow even if an ip is reachable. 但是,即使可以使用ip,使用此工具,我的完整项目也变得非常慢。 After sometime it shows that it is not responding. 一段时间后,它表明它没有响应。 Is there any alternative to this method? 有什么替代方法吗?

Thanks 谢谢

您只需创建一个套接字,然后尝试连接到要检查的节点,如果它抛出exception,则表示该节点当前不可访问。

I have same problem, but I am using another alternative solution if you are using Windows OS . 我有同样的问题,但是 如果您使用的是Windows OS ,则我正在使用另一种替代解决方案。 Simply ping to that IP address ping is respond in millisecond so your app need not wait. 只需ping到该IP地址ping即可在毫秒内响应,因此您的应用程序无需等待。

if ping is respond with Respond texxt than do your stuff. 如果ping是使用Respond texxt进行响应的,则不是您的工作。 Sample code: 样例代码:

Process p = Runtime.getRuntime().exec(new String[]{"ping", your_ip_address});
BufferedReader stdInput = new BufferedReader(new InputStreamReader(p.getInputStream()));

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

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