Как бы я добавил al oop, который подсчитывает текущее количество открытых портов? - PullRequest
0 голосов
/ 30 апреля 2020

Эта программа сканирует мой локальный компьютер и находит все открытые порты. Он возвращает находит какой порт открыт и печатает оператор, который показывает порт. Мне нужно добавить счетчик l oop, который активно подсчитывает текущее количество открытых портов. Мой код выглядит следующим образом:

public class LowPortScanner {

  public static void main(String[] args) {
    final int maxPort = 1024;   // It takes forever to scan all 65536 ports
    String host = "localhost";

    System.err.println("This is an error message");

    if (args.length > 0) {
      host = args[0];
    }
    System.out.println("Scanning ports on " + host + "...");
    for (int i = 1; i <= maxPort; i++) {
      try {
        Socket s = new Socket(host, i);
        // If we get this far, we were able to open the socket. Someone is listening
        System.out.println("There is a something listening on port " + i + " of " + host);
        // Now close it because all we cared about was trying to open it..
        s.close();
      }
      catch (UnknownHostException ex) {
        System.err.println(ex);
        break;
      }
      catch (IOException ex) {
        // There must not be a server on this port
        // We will eat this exception because it will happen too many times.
      }
    }   // end for  
    int portCount = 0;
    for (int i = 0; portCount > 0; i++) {
        portCount = i + portCount;
        System.out.println("There are currently " + i + " ports open");
    }
  }  // end main
}  // end PortScanner

Счетчик, над которым я работал, -

    int portCount = 0;
    for (int i = 0; portCount > 0; i++) {
        portCount = i + portCount;
        System.out.println("There are currently " + i + " ports open");
    }

1 Ответ

0 голосов
/ 30 апреля 2020

Вы должны сосчитать открытые порты, l oop, который вы написали сейчас, не будет ничего полезного.

Вы должны инициализировать счетчик до того, как l oop куда вы идете чтобы проверить все порты.

Затем, когда порт открыт (где вы печатаете, что порт открыт), увеличьте счетчик на 1. После завершения l oop вы можете напечатать счетчик, который имеет общее количество открытых портов.

Это будет выглядеть примерно так:

public class LowPortScanner {

  public static void main(String[] args) {
    final int maxPort = 1024;   // It takes forever to scan all 65536 ports
    String host = "localhost";

    System.err.println("This is an error message");

    if (args.length > 0) {
      host = args[0];
    }
    int portCount = 0; //initialize the counter
    System.out.println("Scanning ports on " + host + "...");
    for (int i = 1; i <= maxPort; i++) {
      try {
        Socket s = new Socket(host, i);
        // If we get this far, we were able to open the socket. Someone is listening
        System.out.println("There is a something listening on port " + i + " of " + host);
        // Now close it because all we cared about was trying to open it..
        portCount++; //port is open so increase counter
        s.close();
      }
      catch (UnknownHostException ex) {
        System.err.println(ex);
        break;
      }
      catch (IOException ex) {
        // There must not be a server on this port
        // We will eat this exception because it will happen too many times.
      }
    }   // end for  
    //print amount of open ports
    System.out.println("There are currently " + portCount + " ports open");

  }  // end main
}  // end PortScanner
...