一.模拟ping的实现
利用InetAddress的isReachable方法可以实现ping的功能,里面参数设定超时时间,返回结果表示是否连上。try...{
InetAddressaddress=InetAddress.getByName("192.168.0.113");
System.out.println(address.isReachable(5000));
}catch(UnknownHostExceptione)...{
e.printStackTrace();
}catch(IOExceptione)...{
e.printStackTrace();
}
二.模拟telnet的实现
利用Socket的connect(SocketAddress endpoint, int timeout)方法可以实现telnet的功能,如果catch到异常说明telnet失败
Socketserver=null;
try...{
server=newSocket();
InetSocketAddressaddress=newInetSocketAddress("192.168.0.201",8899);
server.connect(address,5000);
}catch(UnknownHostExceptione)...{
System.out.println("telnet失败");
}catch(IOExceptione)...{
System.out.println("telnet失败");
}finally...{
if(server!=null)
try...{
server.close();
}catch(IOExceptione)...{
}
}
本文介绍了如何使用Java中的InetAddress类实现模拟Ping功能来检查网络可达性,并通过Socket类实现模拟Telnet功能来测试特定端口的服务可用性。


InetAddressaddress
}


}
546

被折叠的 条评论
为什么被折叠?



