简体   繁体   English

Java,套接字,BufferedReader和readline挂起...... :(

[英]Java, sockets, BufferedReader, and readline hang … :(

I'm not a Java programmer at all. 我根本不是Java程序员。 I try to avoid it at all costs actually, but it is required that I use it for a class (in the school sense). 我试图不惜一切代价避免它,但是我需要将它用于一个班级(在学校意义上)。 The teacher requires that we use Socket(), BufferedReader(), PrintWriter() and various other things including BufferedReader()'s readLine() method. 老师要求我们使用Socket(),BufferedReader(),PrintWriter()和其他各种东西,包括BufferedReader()的readLine()方法。

Basically, this is the problem I'm having. 基本上,这是我遇到的问题。 The documentation clearly states that readLine should return a null at the end of the input stream, but that's not what's happening. 文档清楚地指出readLine应该在输入流的末尾返回一个null,但这不是正在发生的事情。

Socket link       = new Socket(this.address, 80);
BufferedReader in = new BufferedReader( new InputStreamReader( link.getInputStream() ));
PrintWriter   out = new PrintWriter(    new PrintWriter(       link.getOutputStream(), true ));

out.print("GET blah blah blah"); // http request by hand
out.flush(); // send the get please

while( (s=in.readLine()) != null ) {

    // prints the html correctly, hooray!!
    System.out.println(s);
}

Instead of finishing at the end of the HTML, I get a blank line, a 0 and another blank line and then the next in.readLine() hangs forever. 我得到一个空白行,一个0和另一个空白行,然后下一个in.readLine()永远挂起,而不是在HTML的结尾处完成。 Why? 为什么? Where's my null? 哪里是我的空?

I tried out.close() to see if maybe Yahoo! 我试过out.close()看看是否有Yahoo! was doing a persistent http session or something (which I don't think it would without the header that we're willing to do it). 正在做一个持久的http会话或其他东西(我不认为没有我们愿意这样做的标题)。

All the Java sockets examples I'm finding on the net seem to indicate the while loop is the correct form. 我在网上找到的所有Java套接字示例似乎都表明while循环是正确的形式。 I just don't know enough Java to debug this. 我只是不知道足够的Java来调试这个。

Your problem is the content encoding “chunked”. 您的问题是内容编码“chunked”。 This is used when the length of the content requested from the web server is not known at the time the response is started. 当在响应开始时不知道从Web服务器请求的内容的长度时使用此方法。 It basically consists of the number of bytes being sent, followed by CRLF , followed by the bytes. 它基本上由发送的字节数组成,后跟CRLF ,后跟字节。 The end of a response is signalled by the exact sequence you are seeing. 响应的结束由您看到的确切顺序发出信号。 The web server is now waiting for your next request (this is also called “request pipelining”). Web服务器现在正在等待您的下一个请求(这也称为“请求管道传输”)。

You have several possibilities: 你有几种可能性:

  • Use HTTP version 1.0. 使用HTTP版本1.0。 This will cause the webserver to automatically close the connection when a response has been sent completely. 这将导致Web服务器在完全发送响应时自动关闭连接。
  • Specify the “Connection: close” header when sending your request. 发送请求时指定“连接:关闭”标题。 This will also close the connection. 这也将关闭连接。
  • Parse content encoding “chunked” correctly and simply treat this as if the response is now complete—which it is. 正确地解析内容编码“chunked”并简单地将其视为响应现在已完成 - 它是什么。

So you're reading from a socket (you don't show that in your code, but that's what I gather from the text)? 所以你正在从套接字中读取(你没有在你的代码中显示它,但这是我从文本中收集的内容)?

As long as the other side is not closing the connection, Java doesn't know that it's at the end of the input, so readLine() is waiting for the other side to send more data and doesn't return null . 只要另一方没有关闭连接,Java就不知道它在输入的末尾,所以readLine()正在等待另一方发送更多数据并且不返回null

Try GET url HTTP/1.0 . 尝试GET url HTTP/1.0 The HTTP/1.0 tells the server that you can't handle more than a single document per connection. HTTP/1.0告诉服务器每个连接不能处理多个文档。 In this case, the server should close the connection after sending you the result. 在这种情况下,服务器应在发送结果后关闭连接。

Your HTTP request is not complete without 2 carriage return + linefeed pairs. 没有2个回车+换行对,您的HTTP请求就不完整。 You should probably also call close after the request is sent: 你可能也应该在发送请求后调用close:

out.print("GET /index.html HTTP/1.0\r\n");
// maybe print optional headers here
// empty line
out.print("\r\n");
out.flush();
out.close();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM