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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T05:54:50+00:00 2026-05-16T05:54:50+00:00

We are integrating with an external product that requires us to communicate with it

  • 0

We are integrating with an external product that requires us to communicate with it using Java sockets. We’ve been able to read small responses from the server with no issues, but larger responses are causing some headaches.
I made some changes to the socket handling logic and now we seem to be able to read large responses 90% of the time. It does still fail occasionally. Failure, in this case, means that the Java client stops reading from the socket before the entire response has been read. The client thinks that the read operation is finished, and stops normally – there are no exceptions or timeouts involved.
Here’s what the current logic looks like:

StringWriter response = new StringWriter();
PrintWriter writer = new PrintWriter(response);

char[] buf = new char[4096];
int readChars;
do {
   readChars = responseBufferedReader.read(buf);
   writer.write(buf, 0, readChars);
} while(readChars != -1 && responseBufferedReader.ready());

responseBufferedReader is a BufferedReader wrapped around an InputStreamReader wrapped around the Socket‘s InputStream.
This code works most of the time, but it seems like checking for readChars != -1 and ready() are not reliable enough to indicate if we’ve read all of the content from the server. Comparing the number of read characters to the buffer size is also not reliable, since the server seems to be a little slow at sending the response back causing these numbers to differ.
I’ve tried changing the size of the character buffer; it helped, but it’s still not working 100% of the time.
Is there a better and more reliable way to read entirely from a Socket without knowing the size of the expected response? I’ve been doing some research on SocketChannels, but I’m not sure if there’s any benefit to be had by switching.
In case it helps, we’re making a single, blocking Socket connection to the server. The Socket is configured for a 100 second timeout

  • 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-16T05:54:51+00:00Added an answer on May 16, 2026 at 5:54 am

    You shouldn’t be checking whether the BufferedReader is ready() to tell if you’re done. It’s possible that bytes are still being read off of the wire and the BufferedReader has nothing for you, but the Socket is not yet closed.

    I’m not sure what value the BufferedReader is getting you (well, it might help your performance). I would expect the following to work better:

    StringWriter response = new StringWriter();
    PrintWriter writer = new PrintWriter(response);
    
    char[] buf = new char[4096];
    int readChars;
    do {
       readChars = inputStreamReader.read(buf);
       writer.write(buf, 0, readChars);
    } while(readChars != -1);
    

    I think that you’re probably dropping out of the loop when the network is the bottleneck – the loop processes the results fast enough so that the Reader isn’t ready yet and you assume that you’re done reading (even though you’re not).

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

Sidebar

Related Questions

I'm looking at integrating an ESB into an existing Java/Maven web based product. Specifically,
I have a external library that requires a XmlNode[] instead of XmlNodeList. Is there
hello all i have code from open source project that im integrating into my
The company that I work for is redeveloping an in-house product for external use.
I'm currently integrating external applications in my app ex SalesForce.com. My question pertains to
may i know what integration technique that you folks use to implement external component
I am integrating an app with a service (iContact), that describes its API as
I'm working with a certain external server product which happens to be proprietary. However,
MY team and I are working on creating gem libraries that have various external
I am writing a GWT app that involves interacting with an external document in

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.