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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T08:23:32+00:00 2026-05-20T08:23:32+00:00

i am having a bit of trouble with DataInputStreams, So i have data coming

  • 0

i am having a bit of trouble with DataInputStreams,

So i have data coming for a local server, i know that the bytes i read in will follow this format

0x01 to specify it is a string

then random amount of bytes

followed by trailing 0x00 0x00,

i am having trouble reading from the server though,

here is my method for reading

    public static String convertFromServer(DataInputStream dis) throws IOException{
    //Buffer to hold bytes being read in
    ByteArrayOutputStream buf = new ByteArrayOutputStream();

    if(dis.read() != -1){
        //Check to see if first byte == 0x01
    if(dis.read() == 0x01){
        //Check if byte dosnt equal 0x00, i need it to check if it is actually 0x00 0x00
        while(dis.read() != 0x00){
            buf.write(dis.read());
        }
    }

    if(dis.read() == 0x03){    

        while(dis.read() != 0x00){

            buf.write(dis.read());
        }
    }
    }
    String messageRecevied = new String(buf.toByteArray());
    return messageRecevied;

}

If i am a little ambiguous let me know.

i am getting data back , its just not fully correct, basically what i do is send across a byte array, with with the first element being 0x01 to specify string, then the string in bytes and then finally the last 2 elements are 0x00 and 0x00, then this data is then sent back to me from the server, the server definitely receives the data, just when i am reading back it not correct, letter will be missing

this code writes encodes the data i nthe format 0x01, then message in bytes, then 0x00,0x00

  public static void writeStringToBuffer(ByteArrayOutputStream buf,String message){

    buf.write(0x01);

    byte[] b = message.getBytes();

    for(int i =1; i<message.getBytes().length+1;i++ ){

        buf.write(b[i-1]);

     }
    buf.write(0x00);
    buf.write(0x00);


}
  • 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-20T08:23:32+00:00Added an answer on May 20, 2026 at 8:23 am

    Something that always gets me when I’m doing socket work is making sure the orders are reversed on the server and the client.

    If the server reads first, the client has to write first. If the Server writes first, the client has to read first. If they’re both trying to read or both trying to write, you won’t get any output.

    My recommendation is to do this:

    if(dis.read() == 0x01) {
        int zeroesInARow = 0;
    
        while(zeroesInARow < 2) {
            int b = dis.read()
            if(b == 0x00) zeroesInARow++;
            else zeroesInARow = 0;
            buf.write(b);
        }
        String rawMessage = new String(buf.toArray());
        // take off the last two 0s
        String messageRecevied = rawMessage.substring(0,rawMessage.length()-2);
        return messageRecevied;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

having a bit of trouble adding some data to a database. I have the
Im having a bit of trouble with this regex. I have a line that
I'm having a bit of trouble in Google App Engine ensuring that my data
Having a bit of trouble using the List.Find with a custom predicate i have
I'm having a bit of trouble with the mktime function. On my production server,
having a bit of trouble with replacing things that are NOT 0-9A-Za-z[:space] because I
I’m having a bit of trouble controlling the results from a data generating algorithm
I'm having bit of a trouble selecting correct values from my mysql sql server.
Having a bit of trouble. I need to find all of the countries that
I'm having a bit of trouble with this one! I have definitely looked 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.