一.模拟ping的实现
利用InetAddress的isReachable方法可以实现ping的功能,里面参数设定超时时间,返回结果表示是否连上。
try {
InetAddress address = InetAddress.getByName("192.168.0.113");
System.out.println(address.isReachable(5000));
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
二.模拟telnet的实现
利用Socket的connect(SocketAddress endpoint, int timeout)方法可以实现telnet的功能,如果catch到异常说明telnet失败
try {
Socket server = new Socket();
InetSocketAddress address = new InetSocketAddress("192.168.0.113",
8080);
server.connect(address, 5000);
server.close();
} catch (UnknownHostException e) {
System.out.println("telnet失败");
} catch (IOException e) {
System.out.println("telnet失败");
}
欢迎加群互相学习,共同进步。QQ群:iOS: 58099570 | Android: 330987132 | Go:217696290 | Python:336880185 | 做人要厚道,转载请注明出处!http://www.cnblogs.com/sunshine-anycall/archive/2009/06/09/1499896.html