Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8828027
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T07:30:47+00:00 2026-06-14T07:30:47+00:00

I am now writing a http server and I am having problem with reading

  • 0

I am now writing a http server and I am having problem with reading from a socket.
My problem is that the inputStream from the client never ends and it keeps reading until the client is closed.
I know that the client doesn’t close the connection with the server immediately after sending the http request.
How can I quit the while loop when client has sent all the request data (i.e. headers + body).

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";
    }
}

After reading comments and answer from you guys, this is what I came up with,

     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++;
             }

but, I still don’t get it about reading by bytes. I can write my code to read until -1, but still stuck when there is no EOF and client is not closing connection.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T07:30:49+00:00Added an answer on June 14, 2026 at 7:30 am

    I’ve got it 🙂 Thank you guys for comments and answers…

         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
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm writing a UDP server that currently receives data from UDP wraps it up
I've written a DataSnap server in Delphi, and am now writing an iPhone client
i'm writing some kind of application that will send http requests to server. My
Overview of the problem: I've been playing with writing custom http server apps for
I began writing an app using declarative_authorization ( http://github.com/stffn/declarative_authorization ) but I'm now wondering
I am currently writing the specification for an API that works as follows: Client
I thought I had this all figured out, but now that I'm writing a
I'm writing a web server, and I'd like to know what HTTP request headers
I'm writing an app which uses persistent connections over http - that is, the
I am writing the server software that serves mp4 video streams to iphone &

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.