I am trying to connect to a my remote server from my Android device. How do I check if a specific port on my server is open? Eg. how to check if port 80 is open on my server 11.11.11.11?
Currently, I am using InetAddress
to ping if the host is reachable but this does not tell me if the port 80 is open.
Current Code
boolean isAvailable = false;
try {
isAvailable = InetAddress.getByName("11.11.11.11").isReachable(2000);
if (isAvailable == true) {
//host is reachable
doSomething();
}
} catch (Exception e) {
}
Create a Socket that will check that the given ip with particular port could be connected or not.
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;
}
}
Runtime.getRuntime().exec() and pass command to it to get various port entry with pid. Commands are below:
“cat /proc/net/tcp”, “cat /proc/net/tcp6”, “cat /proc/net/udp”, “cat /proc/net/udp6”.
Here is explanation with source code. I have tested it. :
http://kickwe.com/tutorial/android-port-scanner-tutorial/