Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to communicate between php and boost library IPC?
    text
    copied!<p>I have client and server in php communicating over shared memory, Now I would like to access this shred memory object using Boost.Interprocess how can I access it? server.php:</p> <pre><code> function create_image($str){ // Create a blank image and add some text $im = imagecreatetruecolor(300, 20); $text_color = imagecolorallocate($im, 233, 14, 91); $stringBanner=exec("date").$str; imagestring($im, 1, 5, 5, $stringBanner , $text_color); ob_start(); imagejpeg($im); $i = ob_get_contents(); ob_get_clean(); imagedestroy($im); return $i; } echo "\n".__FILE__."\n"; $shm_key = ftok(__FILE__, 't'); echo $shm_key."\n"; $shm_id = shmop_open($shm_key, "a", 0, 0); if ($shm_id) { //it is already created shmop_delete($shm_id); shmop_close($shm_id); } //you need to create it with shmop_open using "c" only echo "try to create\n"; if(!$shm_id = shmop_open($shm_key, "c", 0777, 1024*4))exit(-1); echo "ID ".$shm_id."\n"; $i=0; for(;;){ sleep(1); $s="i=".$i++; $str=$i; $im=serialize(create_image($str)); $data=serialize(strlen($im)); $shm_bytes_written = shmop_write($shm_id, $data, 0); $shm_bytes_written = shmop_write($shm_id, $im, 32); echo $shm_bytes_written." bytes is written: ".$s." ID = $shm_id\n"; } </code></pre> <p>client.php</p> <pre><code>&lt;?php $shm_key =1946222626;// ftok(__FILE__, 't'); $shm_id = shmop_open( $shm_key, "a", 0644,1024*4 ); $s=shmop_size($shm_id); $data = unserialize( shmop_read( $shm_id, 0, 31) ); $im = unserialize( shmop_read( $shm_id, 32, $data) ); // Set the content type header - in this case image/jpeg header('Content-Type: image/jpeg'); // Output the image echo $im; </code></pre> <p>What kind of key I should provide to Boost to get this memory region?</p> <p>boost_client.cpp</p> <pre><code>#include &lt;boost/interprocess/shared_memory_object.hpp&gt; #include &lt;iostream&gt; #include "sys/msg.h" int main() { int msqid; key_t key; char f[]="??????"; int mid; //key = ftok(, 't'); //msqid = msgget(key, 0666 | IPC_CREAT); std::cout&lt;&lt;msqid&lt;&lt;std::endl; boost::interprocess::shared_memory_object shdmem(boost::interprocess::open_or_create, f,//"shmem_server", boost::interprocess::read_write); shdmem.truncate(1024); std::cout &lt;&lt; shdmem.get_name() &lt;&lt; std::endl; boost::interprocess::offset_t size; if (shdmem.get_size(size)) std::cout &lt;&lt; size &lt;&lt; std::endl; } </code></pre> <p><strong>EDIT:</strong></p> <p>Well I found the solution in Boost IPC library Docs:</p> <p><a href="http://www.boost.org/doc/libs/1_49_0/doc/html/interprocess/sharedmemorybetweenprocesses.html#interprocess.sharedmemorybetweenprocesses.sharedmemory.xsi_shared_memory">XSI_KEY based example from boost Docs</a></p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload