700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > Java测试IP和端口是否连通

Java测试IP和端口是否连通

时间:2024-07-12 14:21:27

相关推荐

Java测试IP和端口是否连通

public static boolean pingIp(String ip) throws UnknownHostException, IOException {//能ping通放回true 反之 false 超时时间 3000毫秒return InetAddress.getByName(ip).isReachable(5000);}public static boolean telnetport(String ip, Integer port) throws IOException {Socket connect = new Socket();boolean res = false;try {connect.connect(new InetSocketAddress(ip, port), 1000);//建立连接//能telnet通返回true,否则返回falseres = connect.isConnected();//通过现有方法查看连通状态} catch (IOException e) {e.printStackTrace();System.out.println("false");//当连不通时,直接抛异常,异常捕获即可} finally {try {connect.close();} catch (IOException e) {e.printStackTrace();System.out.println("false");}}return res;}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。