How to write byte by byte to a socket in PHP?

How to write bytes bytes to a socket in PHP?

For example, how can I do something like:

socket_write($socket,$msg.14.56.255.11.7.89.152,strlen($msg)+7); 

Concatenated pseudo-code digits are actually bytes in dec. I hope you understand me.

+4
source share
3 answers

You can use the pack function to pack data into any type of data. Then send it using any socket function.

 $strLen = strlen($msg); $packet = pack("a{$strLen}C7", $msg, 14, 56, 255, 11, 7, 89, 152); $pckLen = strlen($packet); socket_write($socket, $packet, $pckLen); 
+5
source

By http://www.php.net/manual/en/function.socket-create.php#90573

You should be able to do

 socket_write($socket,"$msg\x14\x56\x255\x11\x7\x89\x152",strlen($msg)+7); 
+1
source

Prior to PHP 6, bytes are just characters. Writing a string is the same as writing an array of bytes. If these are decimal values โ€‹โ€‹of the ascii character, you can replace your $ msg ... bit as follows:

 $msg.chr(14).chr(56).chr(255).chr(11).chr(7).chr(89).chr(152) 

If you could explain what you are trying to do, it will be easier for us to provide a more useful answer, but in the meantime it will fulfill what you have described so far.

0
source

Source: https://habr.com/ru/post/1304502/


All Articles