aspose file tools*
The moose likes Sockets and Internet Protocols and the fly likes Receiving ByteArray from the DataInputStream over the network adds malicious bytes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Sockets and Internet Protocols
Bookmark "Receiving ByteArray from the DataInputStream over the network adds malicious bytes" Watch "Receiving ByteArray from the DataInputStream over the network adds malicious bytes" New topic
Author

Receiving ByteArray from the DataInputStream over the network adds malicious bytes

Akshay Viswanathan
Greenhorn

Joined: Dec 01, 2012
Posts: 2
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
Marshal

Joined: Mar 22, 2005
Posts: 41843
    
  63
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?


Ping & DNS - my free Android networking tools app
Akshay Viswanathan
Greenhorn

Joined: Dec 01, 2012
Posts: 2
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Receiving ByteArray from the DataInputStream over the network adds malicious bytes