Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Receiving ByteArray from the DataInputStream over the network adds malicious bytes

 
Akshay Viswanathan
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am building a peer-peer application.
I am sending a fixed size byte array over the network using DataOutputStream and receiving it at the server end using DataInputStream.
Let's say the byte array that is sent, looks something like this { 34, -124, 56, -89}. At the receiving end, I get the following {00, 34, -124, 56}
Essentially, the first byte in the array is replaced by 10 and sometimes by 00 and it slides the other bytes down, hence messing the whole list up.
Also, when i run the the 2 peers locally on my system, this doesn't happen. The moment i run them on 2 different machines over the internet, this thing acts up.
Also lets say A connects to B, then this happens only when i have data to be sent from B-->A and not A-->B. Weird!!!
Can someone tell me the reason this is happening? Or another way to avoid this?
Spent the whole day breaking my head over it. I need a solution urgently.
Thanks in advance.
 
Ulf Dittmer
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Data[In|Out]putStream assumes that you're transferring Java data types over the wire; if you're sending raw bytes then Buffered[In|Out]putStream is a better choice. Also, are you aware of the issue Endianness?
 
Akshay Viswanathan
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Ulf, a response is encouraging, Buffered IN OUT Stream didn't work, but i found a workaround for this.. And i took care of the Endiannes..
The issue is resolved..
Apparently if you write a byte array directly into a dataStream it messes it up. So the way to do it is, write the raw data into a dummy stream object and then call the Stream.getByteArray() method to get the array before writing it on the actual stream.
That worked.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic