Java Language Create file fports with the first line as a hostname that needs to
ID: 3805745 • Letter: J
Question
Java Language
Create file fports with the first line as a hostname that needs to be scanned for certain ports to be opened on it (the values shown are examples... you need to select your own values): host name 80 22 443 445 Write a java program to accomplish the following: Open file fports Read the hostname to be scanned and store it in variable hostname Read the ports into array of integers ports Create array status (of boolean type) Write a for loop to scan one port at a time and make note of its status (true if the port is open... false if the port is closed) Write a for loop to store the result of the scan in file fports _result as follows (the values shown are examples): host name 80 Open 22 Close 443 Open 445 Close Write a for loop to print the result of the scan as wellExplanation / Answer
I tried my best and commented the code for you. In case of difficulties, ping me...
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
/**
*
* @author Sam
*/
public class PortChecker {
public static void main(final String... args) throws InterruptedException, ExecutionException, FileNotFoundException, IOException {
final ExecutorService es = Executors.newFixedThreadPool(20);
BufferedReader br = new BufferedReader(new FileReader("fports"));
final String ip = br.readLine(); //read ip or hostnaem
final int timeout = 200;
System.out.println(ip);
String port;
final List<Integer> ports = new ArrayList<>();
while ((port = br.readLine()) != null) {
ports.add(Integer.parseInt(port)); //read and addd ports
System.out.println(port); //print port
}
final List<Future<Boolean>> futures = new ArrayList<>();
ports.stream().forEach((p) -> {
futures.add(portIsOpen(es, ip, p, timeout)); //try for each saved ports
});
es.shutdown();
System.out.println(" ");
System.out.println(ip);
PrintWriter pw = new PrintWriter("fports_results");
for (int i = 0; i < ports.size(); i++) {
pw.println(ports.get(i) + " " + (futures.get(i).get() ? "open" : "close")); //print port and status as open/clsoe
}
pw.flush();
pw.close();
}
public static Future<Boolean> portIsOpen(final ExecutorService executorService, final String ip, final int port, final int timeout) { //to parallelizeand reduce time
return executorService.submit(() -> {
try (Socket socket = new Socket()) {
socket.connect(new InetSocketAddress(ip, port), timeout);
return true; //if port open,return true
} catch (Exception ex) {
return false; //if closed
}
});
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.