Create a port scanner using java

Create a port scanner using java

Port scanner is a network scan for open ports on a network to determine if those ports need to be closed to provide more network security and less vulnerabilities.

import java.net.*;

public class PortScanner {

    public static void main(String[] args) {

        for (int port = 1; port <= 65535; port++) {
            int timeOut = 1000;
            try {
                Socket socket = new Socket();
                socket.connect(new InetSocketAddress("127.0.0.1", port), timeOut);
                socket.close();
                System.out.println("Port " + port + " is open");
            } catch (Exception e) {
//                    System.out.println("Port " + port + " is closed");
            }
        }

    }
}