Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Networking  RSS feed

Ramesh Pappala
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Iam sending a byte array from server to client by using the below statement,


OutputStream os=s.getOutputStream();
byte[] encryptedData=encrypt(data.getBytes(),publicKey);
// where encrypt function returns a byte array

And in client iam receiving it by the following code:

InputStream is=s.getInputStream();
InputStreamReader isr1=new InputStreamReader(is);
BufferedReader br1=new BufferedReader(isr1);
String data=br1.readLine();

//Again I converted that string value into bytes and printed it

But the byte array that contains a value on the server side is different when compared to the value that i got on client side.

The byte value at server side is:[B@1f3aa07

And the byte value iam getting after receiving the above value into a string variable and converting it back into byte array is: [B@15b9e68

How to solve this problem and how to get the same byte array value from server side to client side. Please, send if there is any code which is illustrating that problem.

Thanking you,
William Brogden
Author and all-around good cowpoke
Posts: 13078
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is because you are using a Reader - all Readers turn byte streams into characters. The readLine method implies characters - it looks for line ends.

Instead use a BufferedInputStream and read into a byte[] buffer.

A better alternative would be to write the encryptedData byte[] as an object and read it as an object on the client side. See the and ObjectInputStream - it really is simple and takes care of details such as adapting to the size of the byte[] that you would have to handle in code.
Ernest Friedman-Hill
author and iconoclast
Posts: 24217
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The strings "[B@15b9e68" have nothing to do with the arrays' contents: it's just what the array object's "toString()" method returns (inherited from java.lang.Object). "[B" indicates this is a byte array, and "15b9e68" is the return value of hashCode() on that same object.

Now: of more concern is why you're sending an array of bytes to an OutputStream, but reading a String from a Reader. It's unlikely you'll get the results you want. On the receiving end, it's likely you want to read the bytes directly from the stream, decrypt them, and then construct your String.

In any case, we're not in "Advanced Java" territory here. I think perhaps our Sockets and Internet Protocols forum is the best home for this thread; I will move it there.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!