Advertisement
8thbit

HostFinder

May 5th, 2015
269
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.43 KB | None | 0 0
  1.  
  2. import java.io.BufferedInputStream;
  3. import java.io.InputStream;
  4. import java.io.OutputStream;
  5. import java.io.PrintWriter;
  6. import java.net.InetSocketAddress;
  7. import java.net.Socket;
  8. import java.util.ArrayList;
  9. import java.util.List;
  10. import java.util.concurrent.Callable;
  11. import java.util.concurrent.ExecutionException;
  12. import java.util.concurrent.ExecutorService;
  13. import java.util.concurrent.Executors;
  14. import java.util.concurrent.Future;
  15. import java.util.concurrent.TimeUnit;
  16. import java.util.logging.Level;
  17. import java.util.logging.Logger;
  18.  
  19.  
  20. /*
  21.  * To change this license header, choose License Headers in Project Properties.
  22.  * To change this template file, choose Tools | Templates
  23.  * and open the template in the editor.
  24.  */
  25. /**
  26.  *
  27.  * @author Oplus
  28.  */
  29. public class main {
  30.  
  31.     /**
  32.      * @param args the command line arguments
  33.      */
  34.     public static void main(String[] args) {
  35.         try {
  36.             main obj = new main();
  37.             obj.run(args);
  38.         } catch (Exception ex) {
  39.             Logger.getLogger(main.class.getName()).log(Level.SEVERE, null, ex);
  40.         }
  41.  
  42.     }
  43.  
  44.     public void run(String[] args) throws Exception {
  45.         String IP = "192.168.1.";
  46.         String Command = "Hello!\r\n";
  47.         ExecutorService executor = Executors.newFixedThreadPool(20);
  48.         List<Future<result>> list = new ArrayList<Future<result>>();
  49.         for (int i = 0; i < 255; i++) {
  50.             String ip = IP + String.valueOf(i);
  51.             Callable<result> task = new scanner(ip, 9092, Command);
  52.             Future<result> submit = executor.submit(task);
  53.             list.add(submit);
  54.         }
  55.         for (Future<result> future : list) {
  56.             try {
  57.                 result r = future.get();
  58.                 if(r.response != null){
  59.                     System.out.println(r.ip + " Said : " + r.response);
  60.                 }
  61.             } catch (InterruptedException e) {
  62.                 e.printStackTrace();
  63.             } catch (ExecutionException e) {
  64.                 e.printStackTrace();
  65.             }
  66.         }
  67.         executor.shutdown();
  68.         executor.awaitTermination(Long.MAX_VALUE,TimeUnit.NANOSECONDS);
  69.         if(list.size() == 0)
  70.             System.out.println("NotFound !");
  71.         else
  72.             System.out.println("Found !");
  73.     }
  74.  
  75.     class result {
  76.  
  77.         public String ip;
  78.         public String response;
  79.     }
  80.  
  81.     class scanner implements Callable<result> {
  82.  
  83.         private String ip;
  84.         private String cmd;
  85.         private int port;
  86.  
  87.         public scanner(String ip, int port, String cmd) {
  88.             this.ip = ip;
  89.             this.cmd = cmd;
  90.             this.port = port;
  91.         }
  92.  
  93.         @Override
  94.         public result call() throws Exception {
  95.             result t = new result();
  96.             t.ip = this.ip;
  97.             String result = SendCommand(this.ip, this.cmd, true);
  98.             t.response = result;
  99.             return t;
  100.         }
  101.  
  102.         private String SendCommand(String IP, String CMD, boolean output) {
  103.             try {
  104.                 Socket socket = new Socket();
  105.                 socket.connect(new InetSocketAddress(IP, port), 200);
  106.  
  107.                 InputStream inputStream = socket.getInputStream();
  108.                 OutputStream outputStream = socket.getOutputStream();
  109.  
  110.                 BufferedInputStream reader = new BufferedInputStream(inputStream);
  111.                 PrintWriter writer = new PrintWriter(outputStream);
  112.                 writer.write(CMD);
  113.                 writer.flush();
  114.                 if (output == true) {
  115.                     int timeout = 0;
  116.                     while (reader.available() <= 0) {
  117.                         Thread.sleep(200);
  118.                         if (timeout == 5) {
  119.                             return null;
  120.                         }
  121.                         timeout++;
  122.                     }
  123.  
  124.                     int BytesRead = 0;
  125.                     byte[] buffer = new byte[1024];
  126.                     String response = "";
  127.                     while ((BytesRead = reader.read(buffer)) != -1) {
  128.                         response += new String(buffer, 0, BytesRead);
  129.                     }
  130.                     if(response.equals("")){
  131.                         return null;
  132.                     }
  133.                     return response;
  134.                 }
  135.                 return null;
  136.  
  137.             } catch (Exception e) {
  138.                 return null;
  139.             }
  140.         }
  141.     }
  142.  
  143. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement