虽然从套接字读取当客户端完成发送请求如何检测?(While reading from socket

2019-08-02 15:58发布

我现在写一个HTTP服务器和我有从套接字读取问题。 我的问题是, inputStream从客户端永远不会结束,并保持阅读,直到客户端关闭。 我知道,客户端不会关闭与服务器的连接发送HTTP请求之后。 我怎样才能退出while loop ,当客户端发送的所有请求数据(即头+体)。

while (in.hasNextLine()) {
    String line = in.nextLine();

    if (line.equals("")){ // last line of request header is blank
        break; // quit while loop when last line of header is reached
    } else {
        request = request + line + "\n";
    }
}

从你们读意见,回答之后,这是我想出了,

     is = incoming.getInputStream();
            os = incoming.getOutputStream();
            in = new Scanner(is);
            out = new DataOutputStream(os);

            RequestHandler rh = new RequestHandler();
            int length = 0;
            while (in.hasNextLine()) {
                String line = in.nextLine();
                if (line.equals("")) { // last line of request message
                                        // header is a
                                        // blank line
                    break; // quit while loop when last line of header is
                            // reached
                }

                if (line.startsWith("Content-Length: ")) { // get the
                                                            // content-length
                    int index = line.indexOf(':') + 1;
                    String len = line.substring(index).trim();
                    length = Integer.parseInt(len);
                }

                request = request + line + "\n";
            }

             byte[] body = new byte[length];
             int i = 0;
             while (i < length) {
             byte b = in.nextByte();
             body[i] = b;
             i++;
             }

但是,我还是不明白有关字节读取。 我可以写我的代码来读取到-1,但是如果没有EOF和客户端不打烊连接仍然停留。

Answer 1:

有检测取决于什么要求您在处理流的末尾3种方式:

  1. 如果是GETHEAD请求,你只需要读取HTTP头,请求,如果它存在的身体通常被忽略,所以当你遇到\r\n\r\n ,到达请求的结束(实际上是请求头)。
  2. 如果它是一个POST方法,读出的Content-Length的报头和读可达Content-Length字节。
  3. 如果它是一个POST方法和Content-Length标头不存在,这是最有可能发生的,一直读到-1返回,这是信号EOF


Answer 2:

我知道了:)谢谢你的意见和答案家伙...

     is = incoming.getInputStream(); // initiating inputStream
            os = incoming.getOutputStream(); // initiating outputStream

            in = new BufferedReader(new InputStreamReader(is)); // initiating
                                                                // bufferReader
            out = new DataOutputStream(os); // initiating DataOutputSteream

            RequestHandler rh = new RequestHandler(); // create a
                                                        // requestHandler
                                                        // object

            String line;
            while ((line = in.readLine()) != null) {
                if (line.equals("")) { // last line of request message
                                        // header is a
                                        // blank line (\r\n\r\n)
                    break; // quit while loop when last line of header is
                            // reached
                }

                // checking line if it has information about Content-Length
                // weather it has message body or not
                if (line.startsWith("Content-Length: ")) { // get the
                                                            // content-length
                    int index = line.indexOf(':') + 1;
                    String len = line.substring(index).trim();
                    length = Integer.parseInt(len);
                }

                request.append(line + "\n"); // append the request
            } // end of while to read headers

            // if there is Message body, go in to this loop
            if (length > 0) {
                int read;
                while ((read = in.read()) != -1) {
                    body.append((char) read);
                    if (body.length() == length)
                        break;
                }
            }

            request.append(body); // adding the body to request


文章来源: While reading from socket how to detect when the client is done sending the request?