• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Receiving ByteArray from the DataInputStream over the network adds malicious bytes

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic