0

いくつかのポートが開いているかどうか、および 80 ポートが開いているかどうかを確認しようとしています - http 要求を送信し、結果をコンソールに表示します。すべてのポートは、独自のスレッドでチェックされます。

このようなリクエストを送ります

public static void send(Socket sock, String host) throws IOException{
    PrintWriter pw = new PrintWriter(sock.getOutputStream());
    pw.println("GET / HTTP/1.1");
    pw.println("Host: " + host);
    pw.println("");
    pw.flush();
} 

クラスTCPClientではそれを使用し、結果をバイトとして返し、コンソールに表示します。

    try {
        sock = new Socket(host, port);
        System.out.println("port " + port + " is in use");
        // send request
        HttpSender.send(sock, host);
        BufferedReader bf = new BufferedReader(new InputStreamReader(sock.getInputStream()));
        StringBuffer response = new StringBuffer(); 
        String line = "";
        while((line = bf.readLine()) != null) {
          response.append(line);
          response.append('\r');
        }
        bf.close();
        return String.valueOf(response).getBytes(); // in method run I show it
    } catch (SocketException e) {
        return ("port " + port + " is free").getBytes();
    }

ポート チェック用のスレッドのプールを作成します。

public class ThreadPool {

private static int MAX_THREADS = 5;
private static String DESTINATION = "http://stackoverflow.com/";
private ExecutorService es = null;

public ThreadPool() {
    es = Executors.newFixedThreadPool(MAX_THREADS);
}

public void perform(int start, int end) throws UnknownHostException {
    for (int i = start; i <= end; i++) {
        Runnable req = new TCPClient(DESTINATION, i);
        es.execute(req);
    }
    es.shutdown();
    while (!es.isTerminated()) {
    }
    ;
    System.out.println("all ports checked!");
}

}

宛先を として設定しwww.stackoverflow.com、そのテキストを含むドキュメントを取得したときit was moved permanently to http://stackoverflow.com/。この目的地を設定すると、UnknownhostException.

問題はどこだ?

4

1 に答える 1