lichtjiang
Programmer
I have a server listening on unix-domain-socket written in java;
I also have 2 clients that write a 2-byte number to the server. One is written in Java. and the other is in Perl (code like $a=chr(0x80); print SOCKET, $a; $a=chr(0x00); print SOCKET, $a).
On the server side, server reads 2 bytes into a byte array to use this 2-byte number as size of some data like this:
DataInputStream dis=new DataInputStream(socket.getInputStream());
byte [] bSize=new byte [2];
int iRead=dis.read(bSize, 0, 2);
if(iRead==2){
int iDataSize=(0x000000ff & bSize[0]) <<8 | (0x000000ff & bSize[1]); //considering an "int" in java is 4-byte and we want to clear sign bit extension in an "int" variable since data size should be used as unsigned
}
Problem is that the server in Java cannot read 2-byte data size correctly from PERL client but there is no problem with client in JAVA. This seems only happens when each byte is bigger than 127. In other words, in 1-byte variable, if its Most Significant Bit is "1", there is some unknown problem occurs. This may be caused by byte order or how Java extends sign bits in a byte variable. In other words, this kind of data type is incompatile between Java and Perl. Is this possible and how to fix this?
I also have 2 clients that write a 2-byte number to the server. One is written in Java. and the other is in Perl (code like $a=chr(0x80); print SOCKET, $a; $a=chr(0x00); print SOCKET, $a).
On the server side, server reads 2 bytes into a byte array to use this 2-byte number as size of some data like this:
DataInputStream dis=new DataInputStream(socket.getInputStream());
byte [] bSize=new byte [2];
int iRead=dis.read(bSize, 0, 2);
if(iRead==2){
int iDataSize=(0x000000ff & bSize[0]) <<8 | (0x000000ff & bSize[1]); //considering an "int" in java is 4-byte and we want to clear sign bit extension in an "int" variable since data size should be used as unsigned
}
Problem is that the server in Java cannot read 2-byte data size correctly from PERL client but there is no problem with client in JAVA. This seems only happens when each byte is bigger than 127. In other words, in 1-byte variable, if its Most Significant Bit is "1", there is some unknown problem occurs. This may be caused by byte order or how Java extends sign bits in a byte variable. In other words, this kind of data type is incompatile between Java and Perl. Is this possible and how to fix this?