-
Notifications
You must be signed in to change notification settings - Fork 0
/
PortScanner.java
39 lines (33 loc) · 974 Bytes
/
PortScanner.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import java.net.Socket;
import java.io.IOException;
public class PortScanner
{
private String targetHost;
//Constructor to initialize the target host
public PortScanner(String targetHost)
{
this.targetHost = targetHost;
}
// Method to scan ports within a specified range
public void scanPorts()
{
String targetHost = this.targetHost;
int minPort = 1;
int maxPort = 65535;
System.out.println("Scanning ports on " + targetHost + "...");
for (int port = minPort; port <= maxPort; port++)
{
try
{
Socket socket = new Socket(targetHost, port);
System.out.println("Port " + port + " is open");
socket.close();
}
catch (IOException e)
{
//Port is likely closed or unreachable
}
}
System.out.println("Port scanning finished.");
}
}