Print Friendly, PDF & Email

We are going to learn how to do a port check programatically with the help of Java.Linux machines have more ports than Windows, for Mac I haven’t really tried the valid port range.

Port Check Algorithm

To perform port checking we will try to connect to all of the ports one by one.

We have usually two port number sections, below 1000 the ports are already reserved to some common sevices such as mail, web, remote desktop, etc.

So having the problem stated we will perform the following steps:

READ ip or host name
   READ minport
   READ maxport
port <- minport
   IF port < maxport
              connect to host
              WRITE "port open"
       ON ERROR
              WRITE "port closed"

Source code

The full source code is composed of 3 files soon I’ll upload it to github.

Port Scanner.Java

package org.javapro;

 * Created by Javatlacati on 12/11/2016.

import java.util.ResourceBundle;
import javax.swing.*;

public class PortScanner {
     * I8n holder
    public static ResourceBundle resourceBundle = ResourceBundle.getBundle("org.javapro.strings");
     * Minimum port value
    private int minport;
     * Maximum port value
    private int maxport;
     * Internet address ip or host name
    private InetAddress ia;

     * Creates a port scanner object
     * @param minport Minimum port value
     * @param maxport Maximum port value
     * @param ia      Internet address ip or host name
    private PortScanner(int minport, int maxport, InetAddress ia) {
        this.minport = minport;
        this.maxport = maxport;
        this.ia = ia;

    /*public PortScanner(int minport, int maxport) {
        new PortScanner(minport, maxport, null);

     * Scans the address specified by is within port range from minport to maxport (inclusive)
    private void scan() throws UnknownHostException {
        if (ia == null) {
            ia = InetAddress.getLocalHost();
        String hostname = ia.getHostName();

        for (int port = minport; port <= maxport; port++) {
            try (Socket ignored = new Socket(ia, port)) {
                System.out.printf(resourceBundle.getString("listening"), port, hostname);
            } catch (IOException ex) {
                // The remote host is not listening on this port
                System.err.printf(resourceBundle.getString("not listening"), port, hostname);

     * Application entry point
     * @param args command line arguments (currently not processed)
    public static void main(String[] args) {

        InetAddress ia = null;
        String host;
        try {

            host = JOptionPane.showInputDialog(resourceBundle.getString("enter ip"));
            if (host != null) {
                ia = InetAddress.getByName(host);
            int min = Integer.valueOf(JOptionPane.showInputDialog(resourceBundle.getString("minimum")));
            int max = Integer.valueOf(JOptionPane.showInputDialog(resourceBundle.getString("maximum")));
            PortScanner ps = new PortScanner(min, max, ia);

        } catch (UnknownHostException e) {
        } catch (NumberFormatException nfe) {

enter\ ip=Enter the ip to scan or leave empty to test your local machine:\n example:
ERR_MSG_VALUE=Invalid value
listening=Server is listening on port %d of %s\n
maximum=Enter the maximum value of the port
minimum=Enter the minimum value of the port
not\ listening=Server is not listening on port %d of %s\n

enter\ ip=Introduce la ip a escanear o deja vacío para probar tu máquina local:\n ejemplo:
ERR_MSG_VALUE=Valor inválido
listening=El servidor está escuchando en el puerto %d de %s\n
maximum=Introduce el máximo valor del puerto
minimum=Introduce el mínimo valor del puerto
not\ listening=El servidor no está escuchando en el puerto %d de %s\n

Leave a Reply

Your email address will not be published. Required fields are marked *