我期待收到 HTTP 响应,然后Socket
关闭它,但它只是坐在那里,在页面返回后永远不会结束。我假设它与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();
}
}