我正在尝试从我的Android设备连接到我的远程服务器.如何检查服务器上的特定端口是否已打开?例如.如何检查我的服务器11.11.11.11上是否打开了端口80?
目前,我正在使用InetAddressping是否可以访问主机,但这并不告诉我端口80是否打开.
现行守则
boolean isAvailable = false;
try {
isAvailable = InetAddress.getByName("11.11.11.11").isReachable(2000);
if (isAvailable == true) {
//host is reachable
doSomething();
}
} catch (Exception e) {
}
Run Code Online (Sandbox Code Playgroud)
小智 9
创建一个Socket,它将检查具有特定端口的给定ip是否可以连接.
public static boolean isPortOpen(final String ip, final int port, final int timeout) {
try {
Socket socket = new Socket();
socket.connect(new InetSocketAddress(ip, port), timeout);
socket.close();
return true;
}
catch(ConnectException ce){
ce.printStackTrace();
return false;
}
catch (Exception ex) {
ex.printStackTrace();
return false;
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
12188 次 |
| 最近记录: |