日期:2014-05-20 浏览次数:20801 次
ExecutorService executor = Executors.newSingleThreadExecutor(); executor.invokeAll(Arrays.asList(new Task(ipHead, tail, 6610)), 200, TimeUnit.MILLISECONDS); executor.shutdown();
import java.net.*; import java.util.Arrays; import java.util.concurrent.Callable; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.TimeUnit; import java.io.*; public class JPortScanner1 { public JPortScanner1(String IP, int port) { } public static void main(String[] args) throws IOException, InterruptedException { String ipHead; int lastPoint; String IP = InetAddress.getLocalHost().getHostAddress(); lastPoint = IP.lastIndexOf('.'); ipHead = IP.substring(0, ++lastPoint); for (int tail = 234; tail < 255; tail++) { ExecutorService executor = Executors.newSingleThreadExecutor(); executor.invokeAll(Arrays.asList(new Task(ipHead, tail, 6610)), 200, TimeUnit.MILLISECONDS); executor.shutdown(); } } } class Task implements Callable<String> { String ipHead; int ipTail; int port; public Task(String ipHead, int ipTail, int port) { this.ipHead = ipHead; this.ipTail = ipTail; this.port = port; } public String call() throws Exception { Socket connect = new Socket(); connect.setSoTimeout(100); try { connect = new Socket(this.ipHead + ipTail, this.port); connect.close(); System.out.println("Open port:" + this.ipHead + ipTail + " " + this.port); } catch (UnknownHostException e) { System.out.println("Unknown Port:" + this.ipHead + ipTail + " " + this.port); } catch (IOException e) { System.out.println("Unknown Port:" + this.ipHead + ipTail + " " + this.port); } System.out.println("Finished!"); return null; } }
import java.io.IOException; import java.net.InetAddress; import java.net.InetSocketAddress; import java.net.Socket; import java.net.UnknownHostException; import java.util.Collection; import java.util.LinkedList; import java.util.concurrent.Callable; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.TimeUnit; public class JPortScanner1 { public static void main(String[] args) throws InterruptedException, UnknownHostException { String ipHead; int lastPoint; String IP = InetAddress.getLocalHost().getHostAddress(); lastPoint = IP.lastIndexOf('.'); ipHead = IP.substring(0, ++lastPoint); ExecutorService executor = Executors.newCachedThreadPool(); Collection<Callable<String>> list = new LinkedList<Callable<String>>(); long start = System.currentTimeMillis(); for (int tail = 1; tail < 255; tail++) { list.add(new Task(ipHead, tail, 8081)); } executor.invokeAll(list); executor.shutdown(); long end = System.currentTimeMillis(); System.out.println("All time: " + (end - start) + " milliseconds"); } } class Task implements Callable<String> { String ipHead; int ipTail; int port; public Task(String ipHead, int ipTail, int port) { this.ipHead = ipHead; this.ipTail = ipTail; this.port = port; System.out.println(ipHead + ipTail + ":" + port); } public String call(){ Socket connect = new Socket(); try { connect.connect(new InetSocketAddress(ipHead + ipTail, port), 100); while(true){ if(connect.isConnected()){ //System.out.println(ipHead + ipTail + ":" + port + " success"); break; } } connect.close(); } catch (IOException e) { //System.out.println(ipHead + ipTail + ":" + port + " failure"); } return null; } }