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

  • Home
  • SEARCH
  • 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 8596075
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T00:35:28+00:00 2026-06-12T00:35:28+00:00

I’m trying to build a simple web server in Java as practice but I’m

  • 0

I’m trying to build a simple web server in Java as practice but I’m running into a very strange issue. When I attempt to read the InputStream from the Socket, sometimes there is no data to be read.
The process I’m following is this:

I create a ServerSocket, listening on port 80, and call accept() to get a Socket. I then send a request from my browser (Firefox) to localhost, which triggers the accept() to return the Socket.

Sometimes, it will read the HTTP request perfectly. Other times, it fails to read any data (read() returns a -1).

Here is some sample code to illustrate what I’m doing, without any exception handling thrown in:

ServerSocket serv = new ServerSocket(80);

while (true)
{
  Socket con = ServerSocket.accept();

  InputStream input = con.getInputStream();

  bytes[] bytes = new bytes[4000000]; // for simplicity, I figured I'd 
                                      // just make the array huge for now

  int bytesRead = input.read(bytes);
  if (bytesRead > 0)
  {
    StringBuffer sBuffer = new StringBuffer(bytesRead);

    for (int i = 0; i < bytesRead; i++)
    {
      sBuffer.append((char) bytes[i]);
    }

    System.out.println(sBuffer.toString());
  }
}

EDIT: I’ve also tried using a BufferedInputStream and BufferedReader to not avail.

  • 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-12T00:35:29+00:00Added an answer on June 12, 2026 at 12:35 am

    One socket can receive more packets. So you should create a new thread for every socket. Something like:

    while(true){
        Socket socket = serv.accept();
        Client client = new Client(socket);
        new Thread(client).start();
    }
    

    Then you need to create the Client class which implements the runable interface.

    Socket socket;
    public Client(Socket socket){
        this.socket = socket;
    }
    public void run(){
        while(socket.isConnected()){
            //here reads the packets from the inputstream
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a French site that I want to parse, but am running into
Seemingly simple, but I cannot find anything relevant on the web. What is the
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am currently running into a problem where an element is coming back from
I would like my Web page http://www.gmarks.org/math_in_e-mail.txt on my Apache 2.2.14 server to display
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but

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.