Java端口扫描器:如何将所有连续关闭的端口打印为一行上的范围?

huangapple go评论146阅读模式
英文:

Java port scanner: How to print all consecutive closed ports as a range on one line?

问题

Here's the translated code portion:

新手在尝试学习如何在Java中创建一个端口扫描器
我希望代码能够获取所有连续关闭的端口并打印一行显示它们已关闭端口1000到4000已关闭”。
以下是我的代码

import java.net.*;
public class KalebPortScanner {

	public static void main(String[] args) {
		String host = "localhost"; // 用要扫描的设备的IP替换localhost

		int startingPort = 0; 
		int endingPort = 65535;
		
		for (int port=startingPort; port<=endingPort; port++) {	
			try {
				Socket socket = new Socket();
				socket.connect(new InetSocketAddress(host, port), 100);
				System.out.println(port + " 开放");
				socket.close();
				int goodPort = port;
			} catch (Exception e) {
				// TODO: 如果端口关闭
				int badPort = port;
				//System.out.println(badPort + " 到 " + port + " 已关闭");
				//System.out.println(port + " 已关闭");
			}
		}
	}
}

This is the translated code section you provided.

英文:

Noob here trying to learn how to make a port scanner in Java.
I want the code to take all consecutive closed ports and print on one line that they are closed, like "ports 1000 to 4000 are closed".
Here is my code so far:

import java.net.*;
public class KalebPortScanner {

	public static void main(String[] args) {
		String host = &quot;localhost&quot;; // replace localhost with IP of device to scan from

		int startingPort = 0; 
		int endingPort = 65535;
		
		for (int port=startingPort;port&lt;=endingPort;port++) {	
		
			try {
			Socket socket = new Socket();
			socket.connect(new InetSocketAddress(host, port),100);
			System.out.println(+port+ &quot; open&quot;);
			socket.close();
		int goodPort = port;
		} catch (Exception e) {
			// TODO: if port is closed
			int badPort = port;
			//System.out.println(+badPort+ &quot; to &quot; +port+ &quot; are closed&quot;);
			//System.out.println(+port+ &quot; closed&quot;);
		}
		}

	}

}

It's basically a mess but I'm trying to understand how I can mark "badPort" as the lower of the closed ports and "goodPort" as the highest one, and then print it. I appreciate all advice for cleaning this up or ways to improve it. Thank you!

Tried making a variable "goodPort" which equals "port" as long as the port is open, and "badPort" which sets itself to "port" if it's closed, and then tries to put them in one System.out.println to give the user the range of closed ports.

答案1

得分: 0

我认为你可以创建一个类来保存端口范围和状态:

public class PortRange {
    public int min;
    public int max;

    public final boolean isPortBusy;

    public PortRange(int minPort, boolean isPortBusy) {
        this.min = minPort;
        this.max = minPort;
        this.isPortBusy = isPortBusy;
    }
}

然后在 main 方法中做如下操作:

List<PortRange> portRanges = new LinkedList<>();
PortRange portRange = null;

for (int port = 0; port < 65536; port++) {
    boolean isPortBusy = isPortBusy(port);

    if (portRange == null) {
        portRange = new PortRange(port, isPortBusy);
    } else if (portRange.isPortBusy == isPortBusy) {
        portRange.max = port;
    } else {
        portRanges.add(portRange);
        portRange = new PortRange(port, isPortBusy);
    }
}

portRanges.add(portRange);

在这段代码中,静态方法 isPortBusy(port) 根据端口是否繁忙返回 true 或 false。

然后遍历集合并显示结果:

portRanges.forEach(range -> System.out.printf("Port range %d-%d : %s%n", range.min, range.max, (range.isPortBusy ? "Busy" : "Free")));

输出示例:

Java端口扫描器:如何将所有连续关闭的端口打印为一行上的范围?

英文:

I think you can create a class to hold the port range and state:

public class PortRange {
    public int min;
    public int max;

    public final boolean isPortBusy;

    public PortRange(int minPort, boolean isPortBusy) {
        this.min = minPort;
        this.max = minPort;
        this.isPortBusy = isPortBusy;
    }
}

Then do something like this in main method:

List&lt;PortRange&gt; portRanges = new LinkedList&lt;&gt;();
PortRange portRange = null;

for (int port = 0; port &lt; 65536; port++) {
    boolean isPortBusy = isPortBusy(port);

    if (portRange == null) {
        portRange = new PortRange(port, isPortBusy);
    } else if (portRange.isPortBusy == isPortBusy) {
        portRange.max = port;
    } else {
        portRanges.add(portRange);
        portRange = new PortRange(port, isPortBusy);
    }
}

portRanges.add(portRange);

In this code, the static method isPortBusy(port) returns true or false depending on whether the port is busy or not.

Then iterate through the collection and display the result:

portRanges.forEach(range -&gt; System.out.printf(&quot;Port range %d-%d : %s%n&quot;, range.min, range.max, (range.isPortBusy ? &quot;Busy&quot; : &quot;Free&quot;)));

Example of output:

Java端口扫描器:如何将所有连续关闭的端口打印为一行上的范围?

huangapple
  • 本文由 发表于 2023年5月30日 05:48:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/76360489.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定