send non-ascii over socket_write

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
roocell
Forum Newbie
Posts: 2
Joined: Sun Oct 04, 2009 6:25 pm

send non-ascii over socket_write

Post 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.
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Re: send non-ascii over socket_write

Post by s.dot »

in b you are writing an array to the socket
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: send non-ascii over socket_write

Post 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".
roocell
Forum Newbie
Posts: 2
Joined: Sun Oct 04, 2009 6:25 pm

Re: send non-ascii over socket_write

Post 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")
Post Reply