07
Nov

Java program to check if a port is open on local system

Java program to check if a port is open on local system
==========================================================

import java.io.IOException;
import java.net.InetAddress;
import java.net.ServerSocket;

public class Check4OpenPortWindows {

	/**
	 * Check if given port is open on windows system netstat -ano | find "4500"
         * netstat –aon | find "<port number>"

         * here o represents process ID. now you can do whatever with the 
         * process ID. To terminate the process, for e.g., use:

         * taskkill /F /pid <process ID>
	 */

	public static void main(String[] args) {
		try {
			ServerSocket socket = new ServerSocket(4500, 0, InetAddress.getLocalHost());
			System.out.println("port available");
		} catch (IOException e) {
			System.out.println("port not available");
		}
	}
}