1

I have a multithreaded program java java socket and I receive the information bizare. like this ¤¤¤¤¤¤23456718900263678722¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤

public void run() 
       {
            try {

                byte[] bs = new byte[64];

                 // read data into buffer
                 dataReception.read(bs);

                 // for each byte in the buffer
                 for (byte b:bs)
                 {
                    // convert byte into character
                    char c = (char)b;

                    // print the character
                    System.out.print(c);
                 }
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
3
  • Please add some info, how it should work, beacuse no one knows what you need. Commented May 27, 2015 at 10:45
  • I receives a stream DataInput and I want to convert has valid data but for now I receive special carractère Commented May 27, 2015 at 11:19
  • 1
    @chemseddineelgarrai Never provide additional information in comments. Always update your question accordingly. Commented May 27, 2015 at 11:34

4 Answers 4

3

The problem is here:

// read data into buffer
dataReception.read(bs);

Read doesn't read exactly that amount of bytes that you want to have in that array. It can read any number of bytes. Therefore you always have to check the return value of the read operation; and only when all expected bytes were read ... you should continue!

The reason that your output looks like garbage is this not that you would be receiving special characters.

What happens is:

  • You create a new array (which is initialized with zeros values).
  • Then you read some bytes, most likely, not enough bytes to fill that array.
  • After the first read, you print that array that now contains the initial zero values; and some bytes that resemble printable characters.

This can be verified by printing your array before reading. You will see that only contains those "special" characters.

Sign up to request clarification or add additional context in comments.

4 Comments

A new byte array is always initialised to zeros.
@EJP Is that true also for local variables? See the comments on stackoverflow.com/questions/3426843/…
It is true for every array of primitives. 'Local variable' doesn't come into it. The array is always on the heap. Similarly, arrays of objects are always initialized to nulls.
@EJP Interestingly enough; my intuition was "an array should be all 0"; but I had a shroud of cloud; so I turned to SO ... instead of running the code myself. Thanks for your correction.
1

If you're expecting exactly 64 byes, use readFully() instead of read(), or at least take some notice of its return value.

Comments

0

Try:

public void run() 
       {
            try {

                byte[] bs = new byte[64];

                 // read data into buffer
                 int readed = dataReception.read(bs);

                 // for each byte in the buffer
                 for (int n=0; n<readed;n++)

                 {
                    byte b=bs[n];

                    // convert byte into character
                    char c = (char)b;

                    // print the character
                    System.out.print(c);
                 }
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

Comments

0

DataInputStream is usually used when textual information need to be shared over socket. Use DataInputStream.readUTF() method if the transferred data is sent using DataOutputStream.writeUTF(String str) from the other end. DataInputStream and DataOutputStream sends two byte(unsigned) length of data before sending actual data.

final String data = din.readUTF();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.