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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T08:45:26+00:00 2026-05-14T08:45:26+00:00

I have a TCP socket client receiving messages (data) from a server. messages are

  • 0

I have a TCP socket client receiving messages (data) from a server.
messages are of the type length (2 bytes) + data (length bytes), delimited by STX & ETX characters.

I’m using a bufferedReader to retrieve the two first bytes, decode the length, then read again from the same bufferedReader the appropriate length and put the result in a char array.

most of the time, I have no problem, but SOMETIMES (1 out of thousands of messages received), when attempting to read (length) bytes from the reader, I get only part of it, the rest of my array being filled with “NUL” characters. I imagine it’s because the buffer has not yet been filled.

char[] bufLen = new char[2];
_bufferedReader.read(bufLen);

int len = decodeLength(bufLen);

char[] _rawMsg = new char[len];
_bufferedReader.read(_rawMsg);

return _rawMsg;

I solved the problem in several iterative ways:

  • first I tested the last char of my array: if it wasn’t ETX I would read chars from the bufferedReader one by one until I would reach ETX, then start over my regular routine. the
    consequence is that I would basically DROP one message.

  • then, in order to still retrieve that message, I would find the first occurence of the NUL char in my “truncated” message, read & store additional characters one at a time until I reached ETX, and append them to my “truncated” messages, confirming length is ok.

it works also, but I’m really thinking there’s something I could do better, like checking if the total number of characters I need are available in the buffer before reading it, but can’t find the right way to do it…

any idea / pointer ?

thanks !

  • 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-14T08:45:27+00:00Added an answer on May 14, 2026 at 8:45 am

    The InputStream read method may return short reads; you must check the return value to determine how many characters were read, and continue reading in a loop until you get the number you wanted. The method may block, but it only blocks until some data is available, not necessarily all the data you requested.

    Most people end up writing a “readFully” method, like DataInputStream, which reads the amount of data expected, or throws an IOException:

    static public int readFully(InputStream inp, byte[] arr, int ofs, int len) throws IOException {
        int                                 rmn,cnt;
    
        for(rmn=len; rmn>0; ofs+=cnt, rmn-=cnt) {
            if((cnt=inp.read(arr,ofs,rmn))==-1) { 
                throw new IOException("End of stream encountered before reading at least "+len+" bytes from input stream"); 
                }
            }
        return len;
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 372k
  • Answers 372k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Enable Trace in the web.config. Browse the application http:////Trace.axd In… May 14, 2026 at 7:11 pm
  • Editorial Team
    Editorial Team added an answer This answer is outdated. We now have the download attribute.… May 14, 2026 at 7:11 pm
  • Editorial Team
    Editorial Team added an answer How big of an application are you planning? Using the… May 14, 2026 at 7:11 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.