1

I am using the netty API to create a Java server which sends if a client programm sends anything to the server a number like 5. It is a integer with 4 bytes. I think it should be the same like writing an integer into an OutputStream. So I want to receive the integer now by php. But if I call socket_read it outputs nothing helpfull because it does not convert the 4 bytes into a integer. It just uses this as text.

If I send a char it works so I need a way in php to convert it.

Or would it be the best to convert all into text clientside, so all is text-based. I think maybe I have to stick with a text-based or binary output.

EDIT: Another question related to this is, let's say I have 4 bytes in java. How can I build a Integer? Do I have to concat the 4 bytes?

Example:

Byte 1: 00000000

Byte 2: 00010000

Byte 3: 00000101

Byte 4: 00000001

Result: 00000000 00010000 00000101 00000001

Would this be correct?

maxammann
  • 1,018
  • 3
  • 11
  • 17

1 Answers1

0

You probably want to use the unpack function. Something like:

$data = socket_read($sock,4);
$x = unpack("N",$data);
david
  • 997
  • 6
  • 15
  • Ah yeah thx this was the method I was looking for and I also found the answer to my second question: http://stackoverflow.com/questions/2383265/convert-4-bytes-to-int – maxammann Jan 24 '13 at 21:43