現在、以下に示すようにリクエストを送信しており、レスポンスを印刷して終了したいと考えていました。応答全体を取得してから終了する堅牢な方法はありますx
かx
? 現在、Scanner
ブロックが次の入力を待っているため、プログラムが終了することはありません。ブロックする可能性のあるある種のループ/リーダーの組み合わせがない場合、他にどのように応答を出力できますか?
public class PingHost {
public static void main(String[] args) throws Exception {
Socket s = new Socket("www.google.com", 80);
DataOutputStream out = new DataOutputStream(s.getOutputStream());
out.writeBytes("GET / HTTP/1.1\n\n");
Scanner sc = new Scanner(s.getInputStream());
while (sc.hasNext())
System.out.println(sc.nextLine());
System.out.println("never gets to here");
s.close();
}
}