• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

Networking

 
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

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

Server:

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

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
System.out.println(data.getBytes());

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,
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 java.io.ObjectOutputStream 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.
Bill
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Not so fast naughty spawn! I want you to know about
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic