Take the 2-minute tour ×

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

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("").isReachable(2000);
    if (isAvailable == true) {
       //host is reachable
} catch (Exception e) {

share|improve this question
I think your app should not care for that. Just display "connection to server died" message if you catch exception thrown because of connection problems. –  hovanessyan Nov 12 '11 at 15:40
yup, the problem is the app hangs when it is taking too long to try to connect. I have mitigated the problem by using a loading screen while it is testing the connection to server. Still not the ideal solution though –  ZXingIT Nov 18 '11 at 18:30
add comment

1 Answer

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);
            return true;

        catch(ConnectException ce){
            return false;

        catch (Exception ex) {
            return false;
share|improve this answer
add comment

Your Answer


