Easy Tutorial
❮ Java Hashmap Getorindex Dir Hidden ❯

Java Example - Check if Port is in Use

Java Example

The following example demonstrates how to check if a port is already in use:

Example

import java.net.*;
import java.io.*;

public class Main {
   public static void main(String[] args) {
      Socket Skt;
      String host = "localhost";
      if (args.length > 0) {
         host = args[0];
      }
      for (int i = 0; i < 1024; i++) {
         try {
            System.out.println("Checking " + i);
            Skt = new Socket(host, i);
            System.out.println("Port " + i + " is in use");
         }
         catch (UnknownHostException e) {
            System.out.println("Exception occurred" + e);
            break;
         }
         catch (IOException e) {
         }
      }
   }
}

The above code outputs the following results when run:

...
Checking 17
Checking 18
Checking 19
Checking 20
Checking 21
Port 21 is in use
Checking 22
Checking 23
Checking 24
...

You can also specify the port for a host:

Main.java File

import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketAddress;
import java.net.SocketTimeoutException;

public class Main {

    public static void main(String[] args) {
        // Check port 80 on localhost
        log(isSocketAliveUitlitybyCrunchify("localhost", 80));

        // Check port 8080 on localhost
        log(isSocketAliveUitlitybyCrunchify("localhost", 8080));

        // Check port 8081 on localhost
        log(isSocketAliveUitlitybyCrunchify("localhost", 8081));

        // Check port 80 on tutorialpro.org
        log(isSocketAliveUitlitybyCrunchify("tutorialpro.org", 80));

        // Check port 443 on tutorialpro.org
        log(isSocketAliveUitlitybyCrunchify("tutorialpro.org", 443));

        // Check port 81 on tutorialpro.org
        log(isSocketAliveUitlitybyCrunchify("tutorialpro.org", 81));
    }

    /**
     * Check if host port is alive
     * 
     * @param hostName
     * @param port
     * @return boolean - true/false
     */
    public static boolean isSocketAliveUitlitybyCrunchify(String hostName, int port) {
        boolean isAlive = false;

        // Create a socket
        SocketAddress socketAddress = new InetSocketAddress(hostName, port);
        Socket socket = new Socket();

        // Timeout setting, in milliseconds
        int timeout = 2000;
log("hostName: " + hostName + ", port: " + port);
try {
    socket.connect(socketAddress, timeout);
    socket.close();
    isAlive = true;
} catch (SocketTimeoutException exception) {
    System.out.println("SocketTimeoutException " + hostName + ":" + port + ". " + exception.getMessage());
} catch (IOException exception) {
    System.out.println("IOException - Unable to connect to " + hostName + ":" + port + ". " + exception.getMessage());
}
return isAlive;
}

private static void log(String string) {
    System.out.println(string);
}

private static void log(boolean isAlive) {
    System.out.println("Is actually in use: " + isAlive + "\n");
}

}

The above code outputs the following results:

hostName: localhost, port: 80
IOException - Unable to connect to localhost:80. Connection refused
Is actually in use: true

hostName: localhost, port: 8080
IOException - Unable to connect to localhost:8080. Connection refused
Is actually in use: false

hostName: localhost, port: 8081
IOException - Unable to connect to localhost:8081. Connection refused
Is actually in use: false

hostName: tutorialpro.org, port: 80
Is actually in use: true

hostName: tutorialpro.org, port: 443
Is actually in use: true

hostName: tutorialpro.org, port: 81
SocketTimeoutException tutorialpro.org:81. connect timed out
Is actually in use: false

Java Examples

❮ Java Hashmap Getorindex Dir Hidden ❯