boolean state = false; try { InetAddress ad = InetAddress.getByName("10.3.1.253"); while (!state) { state = ad.isReachable(5000);// 測試是否能夠達到該地址 if (state) { System.out.println("Network connection is successful, began to connect 10.3.1.253 to login authentication!\n"); } else { System.out.println("Network connection fails, try to reconnect!"); } } } catch (UnknownHostException e) { System.err.println("Connection exception:" + e.getMessage()); } catch (IOException e) { e.printStackTrace(); }