Home arrow static arrow Java Programming [Archive] - socket byte values >127
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - socket byte values >127
This topic has 6 replies on 1 page.

Posts:36
Registered: 6/29/00
socket byte values >127   
Aug 3, 2004 1:42 AM



 
I am using a standard socket to pass byte data between my client and server. This has been working like a charm until now when I need to pass byte data > 127. My intention is to pass a small file accross the socket and write it on the client side - thats all.

I am aware that in Java a byte is signed and can have the values -128 to 127. An unsigned byte can have the values 0 to 255. I am also aware that the ASCII range is 0 to 127 but I just cannot seem to get the correct byte values from this socket.

The server writed the value -115(dec) 8D(hex) - the client reads tha value 63(dec) 3F(hex). I have been fiddling with some byte coversion methods like...
(b & 0x7F) + (((b & 0x80) > 0)?128:0);
(b&0xff);
but none of these will make a difference to the value 63 as read by the client.

I am reading data on the client side into a byte array like this...
byte[] bData = new byte[4096];
int len = in.read(bData);

oh yes, I dont want to use DataOutputStream . I really need to solve this problem using the standard OutputStream using write(byte[])

I have searched the forums and found similar problems but not quite the same.
Any help would be very much appreciated. Thanks
 

Posts:11,200
Registered: 7/22/99
Re: socket byte values >127   
Aug 3, 2004 2:20 AM (reply 1 of 6)



 
You don't need any conversions -- just write the bytes as they are and they will be read intact.

Is there any chance you're converting the bytes to characters at some point?
 

Posts:2,909
Registered: 13.8.2003
Re: socket byte values >127   
Aug 3, 2004 2:21 AM (reply 2 of 6)



 
Just cast into byte:
byte b = (byte)245;
 

Posts:2,909
Registered: 13.8.2003
Re: socket byte values >127   
Aug 3, 2004 2:23 AM (reply 3 of 6)



 
Oops sorry, wasn't really following...

If you're dealing with characters (and character encodings) you might accidentally garble your data somewhere.
 

Posts:36
Registered: 6/29/00
Re: socket byte values >127   
Aug 3, 2004 3:15 AM (reply 4 of 6)



 
You don't need any conversions -- just write the bytes as they are and they will be read intact.

Thats what I thought. I am battling to understand why the data differs. Just two bytes in the destination file are different to the source file. (file size is about 1Kb)

Is there any chance you're converting the bytes to characters at some point?

No charecter conversion. The read code is as follows...
byte[] bData = new byte[4096];
int len = in.read(bData);
outFile.write(bData,0,len);

Thanks for the help. I will carry on trying. Someone suggested I try using carrier pigeons to get the file there. Im thinking I might give that a bash.
 

Posts:11,200
Registered: 7/22/99
Re: socket byte values >127   
Aug 3, 2004 3:47 AM (reply 5 of 6)



 
Your code looks fine, assuming you have those lines in a loop (the read method only reads some bytes from the stream, not all)...

Just two bytes in the destination file are different to the source file.

Could you try transmitting a file that contains all the 256 byte values to that we see what bytes are changed, all the of the hight 128 or only the ones in the range 0x80-0x9F?

Someone suggested I try using carrier pigeons to get the file
there. Im thinking I might give that a bash.

Like the Bergen Linux user group? :)
http://news.bbc.co.uk/1/hi/sci/tech/1321176.stm
 

Posts:36
Registered: 6/29/00
Re: socket byte values >127   
Aug 3, 2004 4:27 AM (reply 6 of 6)



 
The client side seems to have been right all along. The problem was on the ser ver side - bytes were converted to String at some stage before being sent. How embarrassing! Thanks for your help guys, and esp to jsalonen for the link - I enjoyed that.
 
This topic has 6 replies on 1 page.