欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

PHP的消息通信机制测试实例

程序员文章站 2024-04-02 10:00:04
本文实例讲述了php的消息通信机制。分享给大家供大家参考,具体如下:

本文实例讲述了php的消息通信机制。分享给大家供大家参考,具体如下:

<?php error_reporting(e_all&~e_warning&~e_notice);
/**
 * example for sending and receiving messages via the system v message queue
 *
 * to try this script run it synchron/asynchron twice times. one time with ?type=send and one time with ?type=receive
 *
 * @author     thomas eimers - mehrkanal gmbh
 *
 * this document is distributed in the hope that it will be useful, but without any warranty;
 * without even the implied warranty of merchantability or fitness for a particular purpose.
 */
ob_implicit_flush(1);
header('content-type: text/plain; charset=iso-8859-1');
echo "start...\n";
// create system v message queue. integer value is the number of the queue
//$queue = msg_get_queue(100379);
$mesg_key = ftok(__file__, 'm');
$mesg_id = msg_get_queue($mesg_key, 0666);
$queue = $mesg_id;
// sendoptions
$serialize_needed=false; // must the transfer data be serialized ?
$block_send=false;    // block if message could not be send (queue full...) (true/false)
$msgtype_send=1;     // any integer above 0. it signeds every message. so you could handle multible message
             // type in one queue.
// receiveoptions
$msgtype_receive=1;    // whiche type of message we want to receive ? (here, the type is the same as the type we send,
             // but if you set this to 0 you receive the next message in the queue with any type.
$maxsize=100;       // how long is the maximal data you like to receive.
$option_receive=msg_ipc_nowait; // if there are no messages of the wanted type in the queue continue without wating.
             // if is set to null wait for a message.
// send or receive 20 messages
for ($i=0;$i<20;$i++) {
   sleep(1);
  ob_flush();
  flush();
 $message='hello, this is flandy,now is '.date("h:i:s",time());   // transfering data
 // this one sends
  if (isset($_get['type'])&&$_get['type']=='send') {
  if(msg_send($queue,$msgtype_send, $message,$serialize_needed, $block_send,$err)===true) {
   echo "the ".$i." message has been sent, the messge is ".$message."\n";
  } else {
   var_dump($err);
  }
 // this one received
 } else {
   $queue_status=msg_stat_queue($queue);
   echo 'get messages in the queue: '.$queue_status['msg_qnum']."\n";
   print_r($queue_status);
   echo "\n";
  if ($queue_status['msg_qnum']>0) {
   if (msg_receive($queue,$msgtype_receive ,$msgtype_erhalten,$maxsize,$daten,$serialize_needed, $option_receive, $err)===true) {
       echo "received data:".$daten."\n";
   } else {
       var_dump($err);
   }
  }
 }
}
?>

更多关于php相关内容感兴趣的读者可查看本站专题:《php curl用法总结》、《php socket用法总结》、《php网络编程技巧总结》、《php面向对象程序设计入门教程》、《php数组(array)操作技巧大全》、《php字符串(string)用法总结》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总

希望本文所述对大家php程序设计有所帮助。