判断两个服务之间是否离线

public class TelnetTest {

@Test
public void telnetTest(){
long start = System.currentTimeMillis();
boolean flag = telnet("192.1.1.132",8100,1000);
long end = System.currentTimeMillis();

System.out.println(flag ? "通讯正常" : "通讯异常" );
System.out.println(String.format(" 耗时 %s ms", end - start));
}

private boolean telnet(String hostName, int port,int timeout){
Socket socket = new Socket();
boolean connected = false;
try {
SocketAddress address = new InetSocketAddress(hostName,port);
socket.connect(address,timeout);
connected = socket.isConnected();
} catch (IOException e) {
// System.out.println("通讯异常");
} finally {
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return connected;
}
}
posted @ 2023-03-16 12:28  sensen~||^_^|||&  阅读(17)  评论(0)    收藏  举报