public class TelnetTest {标签:判断,服务,socket,离线,System,boolean,connected,println,out From: https://www.cnblogs.com/sensenh/p/17222115.html
@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;
}
}