PHP的消息通信机制测试实例_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程序设计有所帮助。

以上是小编为您精心准备的的内容,在的博客、问答、公众号、人物、课程等栏目也有的相关内容,欢迎继续使用右上角搜索按钮进行搜索php
消息通信
delphi 消息机制实例、消息机制实例、消息缓冲队列通信机制、消息通信机制、java反射机制实例,以便于您获取更多的相关知识。

时间: 2024-09-12 15:30:56

PHP的消息通信机制测试实例_php技巧的相关文章

PHP反射机制用法实例_php技巧

本文实例讲述了PHP反射机制的用法,分享给大家供大家参考之用.具体方法如下: 演示示例代码如下所示: <?php class ClassOne { function callClassOne() { print "In Class One"; } } class ClassOneDelegator { private $targets; function __construct() { $this->target[] = new ClassOne(); } function

php中simplexml_load_file函数用法实例_php技巧

本文实例讲述了php中simplexml_load_file函数用法.分享给大家供大家参考.具体用法分析如下: 在php中simplexml_load_file() 函数把 XML 文档载入对象中之后我们就可以利用由此函数返回的对象进行相关的操作了,下面我们看几个测试实例. 例子,XML文件代码如下: 复制代码 代码如下: <?xml version="1.0" encoding="ISO-8859-1"?>  <note> <to&g

javascript实现任务栏消息提示的简单实例_javascript技巧

javascript实现任务栏消息提示的简单实例 <html> <body onload="blurFunc();"> <script> function blurFunc() { setTimeout("setTitle1()","1000"); } var flag=0; var id = setTimeout("setTitle1()","1000"); var i

php实现的美国50个州选择列表实例_php技巧

本文实例讲述了php实现的美国50个州选择列表.分享给大家供大家参考.具体如下: 这里展示的是php生成的美国50个州的选择列表,自动选择当前州 <select name="state" id="state"> <option value="AL" <?PHP if($state=="AL") echo "selected";?>>Alabama</option&g

php将图片保存为不同尺寸图片的图片类实例_php技巧

本文实例讲述了php将图片保存为不同规格的图片类.分享给大家供大家参考.具体如下: 图片处理类.imagecls.php如下: <?php /** 图片处理类 */ class imagecls { /** * 文件信息 */ var $file = array(); /** * 保存目录 */ var $dir = ''; /** * 错误代码 */ var $error_code = 0; /** * 文件上传最大KB */ var $max_size = -1; function es_i

php判断linux下程序问题实例_php技巧

本文实例讲述了php判断linux下程序问题.分享给大家供大家参考.具体如下: 有时候在服务器上面写一些脚本的时候,经常要放到crontab里面定时运行.时间长了就有一个问题,那就是程序重复运行消耗太多的资源,怎么处理呢?下面璞玉写了两种方法. //第一种:用linux里面的正则匹配 function ifrun($clsname,$bf = 0) { //下面进行检测,如有一个进程正在运行,则不运行 $str=shell_exec("/bin/ps ax > /home/root/&qu

php实现用于计算执行时间的类实例_php技巧

本文实例讲述了php实现用于计算执行时间的类.分享给大家供大家参考.具体如下: 有了这个php类,计算函数或者一段代码的执行时间就简单了 <?php class c_Timer { var $t_start = 0; var $t_stop = 0; var $t_elapsed = 0; function start() { $this->t_start = microtime(); } function stop() { $this->t_stop = microtime(); }

PHP中的reflection反射机制测试例子_php实例

Java类反射应用得非常广泛几乎是所有框架的最核心部分,PHP程序员似乎从不关心反射.尝试着用java的思想去理解php的反射,跟java基本上基本一致.参考了php手册:http://www.php.net/manual/zh/book.reflection.php. ReflectTest.php: <?php   class ReflectTest {       /**      * 用户ID      */     private $userId;       /**      * 用

微信公众号开发之微信公共平台消息回复类实例_php技巧

本文实例讲述了微信公众号开发之微信公共平台消息回复类.分享给大家供大家参考.具体如下: 微信公众号开发代码我在网上看到了有不少,其实都是大同小义了都是参考官方给出的demo文件进行修改的,这里就给各位分享一个. 复制代码 代码如下: <?php /**  * 微信公共平台消息回复类  *  *  */ class BBCweixin{    private $APPID="******";  private $APPSECRET="******";  /*