Java Examples - Check whether a port is already in use

Java 实例Java Examples

The following example demonstrates how to detect whether 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("Check"+ i); Skt = new Socket(host, i); System.out.println("Port" + i + "Already in use"); } catch (UnknownHostException e) { System.out.println("Exception occured"+ e); break; } catch (IOException e) { } } } }

The output of the above code is:

……
查看 17
查看 18
查看 19
查看 20
查看 21
端口 21 已被使用
查看 22
查看 23
查看 24
……

You can also specify the host and port:

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 local port 80 log(isSocketAliveUitlitybyCrunchify("localhost", 80)); //Check local port 8080 log(isSocketAliveUitlitybyCrunchify("localhost", 8080)); //Check local port 8081 log(isSocketAliveUitlitybyCrunchify("localhost", 8081)); //Check port 80 of example.com log(isSocketAliveUitlitybyCrunchify("example.com", 80)); //Check port 443 of example.com log(isSocketAliveUitlitybyCrunchify("example.com", 443)); //Check port 81 of example.com log(isSocketAliveUitlitybyCrunchify("example.com", 81)); } /** * Check the host port * * @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("Whether it is actually in use:" + isAlive + "\n"); } }

The output of the above code is:

hostName: localhost, port: 80
IOException - Unable to connect to localhost:80. Connection refused
是否真正在使用: true

hostName: localhost, port: 8080
IOException - Unable to connect to localhost:8080. Connection refused
是否真正在使用: false

hostName: localhost, port: 8081
IOException - Unable to connect to localhost:8081. Connection refused
是否真正在使用: false

hostName: example.com, port: 80
是否真正在使用: true

hostName: example.com, port: 443
是否真正在使用: true

hostName: example.com, port: 81
SocketTimeoutException example.com:81. connect timed out
是否真正在使用: false

Java 实例Java Examples

Other Extensions