Page 1 of 1

send non-ascii over socket_write

Posted: Sun Oct 04, 2009 6:32 pm
by roocell
I need to send a null terminated string over a socket, such that i get an ascii string followed by 0x00.
i've only been able to send valid ascii characters using socket_write.

i've tried:
a) tell socket write to send an extra byte by: socket_write($socket, $string, strlen($string)+1) but that doesnt send the 0x00 i need as well.
b) socket_write fails in this case

Code: Select all

$xml=str_split($xml);
$xml[]=0x00;
if (!socket_write($user->socket, $xml)) logger("Failed to write to socket ".socket_last_error());
 
c) appends a '0' character to my msg

Code: Select all

$xml[strlen($xml)]=0x00;
 
i could really use some help. surely people send non-printable characters over a socket all the time.

Re: send non-ascii over socket_write

Posted: Sun Oct 04, 2009 6:39 pm
by s.dot
in b you are writing an array to the socket

Re: send non-ascii over socket_write

Posted: Sun Oct 04, 2009 8:32 pm
by requinix
In C you can use the strlen+1 trick because C stores strings with a null at the end.
PHP does not do that.

I don't see why you're trying to use arrays. There's no point. Just deal with strings: send $xml . "\0".

Re: send non-ascii over socket_write

Posted: Mon Oct 05, 2009 12:44 pm
by roocell
thank you that did the trick. i figured it was something simple like that.

note: $xml.'\0' doesnt work. it has to be double quotes ($xml."\0")