2

hey all i am trying to make a data output stream in php to write back primitive data types to a java application

i created a class that write the data to an array (write it same as java do , copy from java code)

and finally i am writing back the array to the client.

feels like its not working well

for example the writeInt method send to the java client some wrong values am i doing ok ?

thank you

here is my code

private $buf   = array();


public function writeByte($b) {
  $this->buf[] = pack('c' ,$b);
}

public function writeInt($v) { 
  $this->writeByte($this->shiftRight3($v , 24) & 0xFF);
  $this->writeByte($this->shiftRight3($v , 16) & 0xFF);
  $this->writeByte($this->shiftRight3($v ,  8) & 0xFF);
  $this->writeByte($this->shiftRight3($v ,  0) & 0xFF);

}


private function shiftRight3($a ,$b){
  if(is_numeric($a) && $a < 0){
    return ($a >> $b) + (2<<~$b);
  }else{
    return ($a >> $b);
  }
}


public function toByteArray(){
    return $this->buf;
}

this is how i am setting the main php file

   header("Content-type: application/octet-stream" ,true);
   header("Content-Transfer-Encoding: binary" ,true);

this is how i am returning the data

  $arrResult = $dataOutputStream->toByteArray();
  for ($i = 0 ; $i < count($arrResult) ; $i ++){
     echo $arrResult[$i];
  }

I EDIT THE QUESTION ,ACCOURDING TO MY CODE CHANGING in the java client side seems that i have 2 bytes to read start always i have 13 , 10 , which is \r \n how come i am reading them always ?

(in my test i am sending one byte to the java client side ,

  URL u = new URL("http://localhost/jtpc/test/inputTest.php");
  URLConnection c = u.openConnection();

  InputStream in =  c.getInputStream();
  int read = 0;
  for (int j = 0; read != -1 ; j++) {
    read = in.read();
    System.out.println("More to read : " + read);
  }
 )

  the output will be ,
   More to read : 13
   More to read : 10
   More to read : 1 (this is the byte i am sending)
3
  • Is there a particular reason you're doing it this way instead of using an intermediary data transport, like JSON, WDDX, or plain old XML? Commented Jul 25, 2010 at 5:18
  • yes there is , i have a mechanism for a network protocol me and friend implemented in java ages ago , and i prefer this protocol on top of other things Commented Jul 25, 2010 at 5:29
  • GOT IT WEIRD.... i thing i got my sample working i wrote 3 different int values and its look fine what i had to do is ignore to first 2 bytes that i am reading , can some one help me why ? i update the post on how i am sending byte now Commented Jul 26, 2010 at 20:33

3 Answers 3

3

Php has pack() function for turning data into binary form. Unpack() reverses the operation.

$binaryInt = pack('I', $v);
Sign up to request clarification or add additional context in comments.

7 Comments

my methods work when i print the values to log before sending them just store the data as binary data and send it wont work for me . i tried to use pack but this wont helping echo the data back has strange value in java printing them in php side looks ok so i guess this is just the binary thing
@shay - how are you "echoing back" in Java? I wouldn't expect binary data to be readable if read as bytes and converted / printed as a String.
@jmz i use pach('c' ,$v) , working great i have another problem now
*pack working great ,my new problem is why i am always reading \r\n from the php side
@Shay: I'm sorry but I don't quite follow what you mean by reading \r\n from the php side? Do you receive a CRLF when you expect something else, or the actual string backslash-r-backslash-n?
|
3

The one thing that strikes me as odd is that you are setting the content type to application/zip, but you don't seem to be creating a ZIP encoded output stream. Is this an oversight ... or does PHP perform the encoding for you without you asking?

EDIT

According to RFC 2046, the recommended content type for a binary data format whose content type is not standardized is "application/octet-stream". There is also a practice of defining custom content subtypes with a name starting with "x-" (for experimental), but RFC 2046 says that this practice is now strongly discouraged.

7 Comments

just trying to force it to some binary data . there is a generic content type for raw data ?
i tried this header("Content-type: application/octet-stream"); header("Content-Transfer-Encoding: binary"); not helping see my comment to previous post
@shay - Oh well. But you needed to fix it anyway. Otherwise any client (e.g. browser) that that paid attention to content-type headers would attempt to treat the response as ZIP file!
hey thank you Stephen C , i was able to echo the bytes data back to java , using java DataInputStream to read the data types , but i have to read the two first bytes ,in order to start reading primitive data type , before i am echoing back in php i see that my array length is 4 , in java side i need to read first 2 bytes , then read the INT (which is 4 bytes)
also i update my question for what i am doing , the 4 bytes that i am reading is the the source code of DatainputStream.readInt();
|
1

You don't need that shiftRight3() method, just use >>, as you are masking the result, and then turning it into a chr(). Throw it away.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.