Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

perl client writes 2-byte number through socket to server in java?

Status
Not open for further replies.

lichtjiang

Programmer
Feb 26, 2007
39
US
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 figured this out.

replace "PRINT" with "Syswrite" and use "BINMODE" to specify binary mode for SOCK.

I've not read doc about these guys to figure out WHY this is so. Anyone has experienced this b4 or an idea?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top