views:

75

answers:

3

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)
+1  A: 

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.

EJP
+2  A: 

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

$binaryInt = pack('I', $v);
jmz
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
@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.
Stephen C
@jmz i use pach('c' ,$v) , working great i have another problem now
shay
*pack working great ,my new problem is why i am always reading \r\nfrom the php side
shay
@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?
jmz
i am always reading backslash-r-backslash-n before the data arrivesso in java side i must in.read(); in.read(); , before reading my content
shay
I quess the PHP side has newlines in some script files. Most common cause is terminating `?>` followed by newlines. These are printed by PHP.
jmz
+3  A: 

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.

Stephen C
just trying to force it to some binary data .there is a generic content type for raw data ?
shay
i tried thisheader("Content-type: application/octet-stream");header("Content-Transfer-Encoding: binary"); not helping see my comment to previous post
shay
@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!
Stephen C
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)
shay
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();
shay
@shay - honestly, I haven't a clue. I'm not a PHP developer. All I can say is that you have convinced me that the problem is not on the Java side. I suggest you pay attention to what the other answers said.
Stephen C
well after debugin ,more i am always reading 2 unnecessary bytes 13 , and 10 , they ar \r\n ,where is this \r\n came from ?
shay