简体   繁体   中英

Checking if server port is open from Android

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/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM