Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.BufferedInputStream;
- import java.io.InputStream;
- import java.io.OutputStream;
- 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;
- import java.util.concurrent.TimeUnit;
- import java.util.logging.Level;
- import java.util.logging.Logger;
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- /**
- *
- * @author Oplus
- */
- public class main {
- /**
- * @param args the command line arguments
- */
- public static void main(String[] args) {
- try {
- main obj = new main();
- obj.run(args);
- } catch (Exception ex) {
- Logger.getLogger(main.class.getName()).log(Level.SEVERE, null, ex);
- }
- }
- public void run(String[] args) throws Exception {
- String IP = "192.168.1.";
- String Command = "Hello!\r\n";
- ExecutorService executor = Executors.newFixedThreadPool(20);
- List<Future<result>> list = new ArrayList<Future<result>>();
- for (int i = 0; i < 255; i++) {
- String ip = IP + String.valueOf(i);
- Callable<result> task = new scanner(ip, 9092, Command);
- Future<result> submit = executor.submit(task);
- list.add(submit);
- }
- for (Future<result> future : list) {
- try {
- result r = future.get();
- if(r.response != null){
- System.out.println(r.ip + " Said : " + r.response);
- }
- } catch (InterruptedException e) {
- e.printStackTrace();
- } catch (ExecutionException e) {
- e.printStackTrace();
- }
- }
- executor.shutdown();
- executor.awaitTermination(Long.MAX_VALUE,TimeUnit.NANOSECONDS);
- if(list.size() == 0)
- System.out.println("NotFound !");
- else
- System.out.println("Found !");
- }
- class result {
- public String ip;
- public String response;
- }
- class scanner implements Callable<result> {
- private String ip;
- private String cmd;
- private int port;
- public scanner(String ip, int port, String cmd) {
- this.ip = ip;
- this.cmd = cmd;
- this.port = port;
- }
- @Override
- public result call() throws Exception {
- result t = new result();
- t.ip = this.ip;
- String result = SendCommand(this.ip, this.cmd, true);
- t.response = result;
- return t;
- }
- private String SendCommand(String IP, String CMD, boolean output) {
- try {
- Socket socket = new Socket();
- socket.connect(new InetSocketAddress(IP, port), 200);
- InputStream inputStream = socket.getInputStream();
- OutputStream outputStream = socket.getOutputStream();
- BufferedInputStream reader = new BufferedInputStream(inputStream);
- PrintWriter writer = new PrintWriter(outputStream);
- writer.write(CMD);
- writer.flush();
- if (output == true) {
- int timeout = 0;
- while (reader.available() <= 0) {
- Thread.sleep(200);
- if (timeout == 5) {
- return null;
- }
- timeout++;
- }
- int BytesRead = 0;
- byte[] buffer = new byte[1024];
- String response = "";
- while ((BytesRead = reader.read(buffer)) != -1) {
- response += new String(buffer, 0, BytesRead);
- }
- if(response.equals("")){
- return null;
- }
- return response;
- }
- return null;
- } catch (Exception e) {
- return null;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement