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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T07:24:04+00:00 2026-06-16T07:24:04+00:00

As I’m learning about networking and io in Java, I’m slowly building client/server apps

  • 0

As I’m learning about networking and io in Java, I’m slowly building client/server apps to apply what I’m reading on different tutorials. I’m stumped though, and I’ve been trying to figure out why my code isn’t working for a long time. So I decided to turn to SO’s infinite wisdom 🙂

After accepting the client socket connection from localhost, I have this on my server:

BufferedInputStream in = new BufferedInputStream(socket.getInputStream(),Config.BUFFER_SIZE_NET);
BufferedOutputStream out = new BufferedOutputStream(socket.getOutputStream(),Config.BUFFER_SIZE_NET);
String msg = "";
byte buffer[] = new byte[Config.BUFFER_SIZE_READ];
int bytesRead;
System.out.println("Server is waiting for data");
while ((bytesRead = in.read(buffer)) > 0) {
    msg = msg + new String(buffer,Config.CHARSET);
}
System.out.println("Server received: "+msg);

After connecting to the server, this is executed on the client after I press a JButton:

BufferedInputStream in = new BufferedInputStream(socket.getInputStream(),Config.BUFFER_SIZE_NET);
BufferedOutputStream out = new BufferedOutputStream(socket.getOutputStream(),Config.BUFFER_SIZE_NET);
String msg = "msg";
try{
    out.write(msg.getBytes(Config.CHARSET));
    out.flush();
    System.out.println("Client sent: "+msg);
}catch(Throwable e){e.printStackTrace();}

After pressing the button on the client, I get this output:

Client sent: msg

On the server side, I get:

Server is waiting for data

If I debug the server, I see it blocked forever on the following line:

while ((bytesRead = in.read(buffer)) > 0) {

No exceptions are thrown. What am I missing here? I had it working before but I’ve done many changes and now I can’t get it back to work.

Note: This is a slightly modified version of the actual code, to make it easier to be reviewed. If you think there’s something relevant missing, let me know!

  • 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-16T07:24:05+00:00Added an answer on June 16, 2026 at 7:24 am

    I got out of the hole I dug myself into. It turns out I wasn’t checking for the end on the data being received, so the code was stuck either blocking for reads, or in the loop that followed. By checking for a specific sequence of bytes in the data read by the server, inside the while loop, I was able to identify the end of the data and break from the loop.

    I don’t know if this is the (most) correct answer to the problem above but I solved it in this way, so I’m submitting it as an answer.

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

Sidebar

Related Questions

I am reading a book about Javascript and jQuery and using one of the
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
I've tracked down a weird MySQL problem to the two different ways I was
I don't have much knowledge about the IPv6 protocol, so sorry if the question
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I have been unable to fix a problem with Java Unicode and encoding. The
I have thousands of HTML files to process using Groovy/Java and I need to
I would like my Web page http://www.gmarks.org/math_in_e-mail.txt on my Apache 2.2.14 server to display

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.