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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T08:06:17+00:00 2026-06-09T08:06:17+00:00

I have a puzzling problem when using Java 7 and SSL sockets. I’ve got

  • 0

I have a puzzling problem when using Java 7 and SSL sockets.

I’ve got a client/server bundle. Both send XML data to each other by using a very simple protocol. The first 4 bytes of each message always contain the length of the whole message. That means, it’s the length of the XML data plus 4 bytes.

At first, the client sends a greeting message to the server. Next, the server interprets the greeting and sends a response. Then, the client itself interprets that message and sends its login information. The server checks that information and sends a response back. But this time, the client does not receive anything, although it uses the very same method as when getting the greeting response from the server.

This is the simplified reading-method on the client side:

private String readResponse() throws Exception
{
    BufferedInputStream inputBuffer = new BufferedInputStream(sslSocket.getInputStream());

    // Read and interprete the first 4 bytes to get the length of the XML-data
    int messageLength = readMessageLength(inputBuffer) - 4;

    // PROBLEM OCCURS HERE!

    // Read bufferSize bytes from stream
        byte[] byteArray = new byte[messageLength];
    inputBuffer.read(byteArray);

    // Return the XML-data
    return new String(byteArray);
}

And here the method to retrieve the length from the first 4 bytes…

private int readMessageLength(BufferedInputStream in) throws IOException {
    byte[] b = new byte[4];
    in.read(b, 0, 4);

    return (((b[0] & 0xff) << 24) | ((b[1] & 0xff) << 16) | ((b[2] & 0xff) << 8) | (b[3] & 0xff));
}

At the time the second response should be read by the client, the inputBuffer contains zeros only ([0, 0, 0, 0, 0, 0, ...]). So when trying to calculate the message length using readMessageLength(), it returns -4. And that, of course, leads to an exception.

It seems obvoius that the data sent by the server is not yet ready to be read by the client. Therefore, I made some changes:

int messageLength;
do {
    messageLength = readMessageLength(inputBuffer) - 4;
} while(messageLength <= 0);

However, that didn’t work either. And now comes the puzzling part: What happens is that this loop is executed twice! The value assigned to messageLength now comes from the first 4 bytes of the XML data. The string returned by the reading-method now starts with ?xml, where the < is missing!

So, I tried mark and reset the first for bytes:
inputBuffer.mark(4);

int messageLength;
do {
    inputBuffer.reset();
    messageLength = readMessageLength(inputBuffer) - 4;
} while(messageLength <= 0);

But that is actually an endless-loop. Since the contents of the inputBuffer never changes, messageLength never becomes anything else than -4.

When can I be sure that the data sent to client is available? How could I check that?
What might be a better approach?

Regards,
Walter

P.S. The method available() is not useful as it seems to always return zero when using SSL sockets.

  • 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-09T08:06:19+00:00Added an answer on June 9, 2026 at 8:06 am

    You must absolutely, always check the number of bytes that are read from a communications channel. It can and will be zero at times, and you must keep reading in that case. End of stream has only been reached with a negative return value.

    Specifically, in.read(new byte[100]) will not necessarily read 100 bytes.

    If you want to guarantee you are getting everything, you need to use readFully.

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

Sidebar

Related Questions

I'm using Qt4.6, 32bit compiled under 64bit Linux, and have a puzzling memory problem
I have a small problem with a SWT.Browser widget who throws a quite puzzling
I have a puzzling problem with trying to make an ajax/static state program. What
I have a really puzzling problem: I am writing a PHP CLI application running
This problem is really puzzling to me: I have the following script working on
I have an puzzling problem. I have a new ASP.NET web application in VS2005
I have been puzzling over a problem this morning with LinqToSQL. I'll try and
I am new to socket programming and ran into a puzzling problem: I have
I have another puzzling problem. I need to read .xls files with RODBC. Basically
I have a puzzling problem. I have a pointer to a vanilla NSView that

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.