3

現在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がなく、クライアントが接続を閉じていない場合でもスタックします。

4

3 に答える 3

6

処理しているリクエストに応じて、ストリームの終了を検出する3つの方法があります。

  1. GETまたはリクエストの場合はHEAD、HTTPヘッダーを読み取るだけで済みます。リクエストの本文が存在する場合は通常無視されるため、に遭遇すると\r\n\r\n、リクエストの最後(実際にはリクエストヘッダー)に到達します。
  2. POSTメソッドの場合はContent-Length、ヘッダーのを読み取り、最大Content-Lengthバイトを読み取ります。
  3. それがPOSTメソッドであり、Content-Lengthヘッダーが存在しない場合(これは発生する可能性が最も高い)、が返されるまで読み取ります。-1これは、のシグナルですEOF
于 2012-11-13T02:09:18.877 に答える
4

私はそれを手に入れました:)コメントと回答をありがとうございます...

     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
于 2012-11-13T16:37:47.163 に答える