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 7582241
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T18:23:16+00:00 2026-05-30T18:23:16+00:00

As a challenge I have implemented a basic web server in Java. When I

  • 0

As a challenge I have implemented a basic web server in Java. When I open the raw InputStream I immediately go into a blocking read which reads the entire 400 or bytes of the HTTP request into a byte array. This works, however I then do not check for any more data and simply close the socket after sending a response.

I’m wondering is there a more robust way to do this so as not to miss any data from the client. I thought of reading one byte at a time until read returned end of stream. However it instead sometimes blocks when there is no more data and confusingly the JavaDocs for public abtract int InputStream.read() says:

If no byte is available because the end of the stream has been reached, the value -1 is returned. This method blocks until input data is available, the end of the stream is detected, or an exception is thrown.

So it implies that two things can happen if the end of stream is reached: returning -1 and blocking. I am seeing blocking.

My question is, with a protocol like HTTP, how should you read from the socket and how do you know when that’s all the data you’ll be getting in this 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-05-30T18:23:18+00:00Added an answer on May 30, 2026 at 6:23 pm

    The JavaDoc you quote does not imply that two things can happen if the end of stream is reached. It doesn’t say read blocks when the end of the stream is detected, but until the end of stream is detected. Once it is detected, -1 is returned.

    This explains the behavior you’re observing: no end of stream is detected and read is blocked. The end of stream is detected once the connection is closed, but it isn’t closed since the client doesn’t close the connection immediately after sending a request. It must keep it open to receive the reply.

    In order to ensure you receive all data from the client you should parse their HTTP request until you see the end of header (double newline) plus whatever amount of data they have specified in the header (if any).

    If you’d like to avoid blocking have a look at java.nio and channels (SocketChannel in particular).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I challenge you :) I have a process that someone already implemented. I will
I have a challenge in front of me. Let me present the challenge which
I have a pretty basic challenge-based iPhone game, and I wanted to know what
We have implemented our domain model using the accountability pattern, which we are trying
Challenge: I have this code that fails to compile. Can you figure out what's
For those who like a good WPF binding challenge: I have a nearly functional
I have a challenge I need some input on. I am currently recruiting programmers
I have a challenge that I am trying to solve and I can't work
I have a SQL challenge that is wracking my brain. I am trying to
I have the following challenge, and I haven't found a good answer. I am

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.