<?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);
      }
    }
  }
}

?>

转载于:https://blog.51cto.com/flandycheng/313405

PHP消息通信机制测试代码相关推荐

  1. python 消息机制_Python并发编程之线程消息通信机制任务协调(四)

    . 前言 前面我已经向大家介绍了,如何使用创建线程,启动线程.相信大家都会有这样一个想法,线程无非就是创建一下,然后再start()下,实在是太简单了. 可是要知道,在真实的项目中,实际场景可要我们举 ...

  2. python线程通信 消息传递_Python并发编程之线程消息通信机制/任务协调(四)

    大家好,并发编程进入第四篇. 本文目录 前言 Event事件 Condition Queue队列 总结 .前言 前面我已经向大家介绍了,如何使用创建线程,启动线程.相信大家都会有这样一个想法,线程无非 ...

  3. 最重要的事情 一 、消息通信机制(1)ant 打包方法(2) system.out.println()用法 二、UML学习

    最重要的事情    一 .消息通信机制(1)ant 打包方法(2) system.out.println()用法      二.UML学习

  4. NSNotificationCenter消息通信机制介绍(KVO)

    作用:NSNotificationCenter是专门供程序中不同类间的消息通信而设置的. 注册通知:即要在什么地方接受消息 [[NSNotificationCenter defaultCenter]  ...

  5. iphone ios 消息通信机制NSNotificationCenter

    ObjectC NSNotificationCenter是专门供程序中不同类间的消息通信而设置的,使用起来极为方便, 设置通知,就是说要在什么地方(哪个类)接受通知,一般在初始化中做. [[NSNot ...

  6. RabbitMQ 消息确认机制confirm代码编写

  7. Heartbeat的可靠消息通信实现分析

    Heartbeat的可靠消息通信实现分析 广东省Linux公共服务技术支持中心  颜定成 摘要 Heartbeat项目是Linux-HA工程的一个组成部分,它实现了一个高可用集群系统.心跳服务和集群通 ...

  8. 浅议NetMQ常见模式和消息加密机制

    题图来自https://www.pexels.com/ 概述 在传统企业级开发中,消息队列机制已经成为一种非常常见的技术实现手段,而基于NetMQ则看起来有点像一朵"奇葩",看起来 ...

  9. android消息队列模型,Android 消息队列机制

    在非UI线程使用Handler进行线程通信时,一般都需要进行3个步骤: 创建Looper Looper.prepar() 创建Handler 启动消息循环Looper.loop() 通过这3步,基本就 ...

最新文章

  1. 011_CSS子元素选择器
  2. 学习笔记Hadoop(十)—— Hadoop基础操作(2)—— HDFS常用Shell操作
  3. jar包天天见,可是你知道它的运行机制吗
  4. 阿里第二代微服务强势崛起,你还敢往简历上写“精通”吗?
  5. 【删库不跑路】—Linux系统自杀实验 rm -rf /* 及如何抢救和预防
  6. VC++6遇到的问题(持续更新)
  7. 6款强烈推荐的PDF阅读器
  8. 计算机在数据处理方面的论文,数据挖掘论文3000字范文参考(2)
  9. cadcene17.4软件汉化
  10. dtype 和 shape
  11. ALFA机器视觉深度学习外观检测自学习人工智能软件——红色工具
  12. 编写一个能将给定非负整数列表中的数字排列成最大数字的函数。例如,给定[50,2,1,9],最大数字为95021。
  13. 使用pt-stalk诊断MySQL问题
  14. python ocr高精度识别_OCR识别-python版(一)
  15. ECB(实体边界控制)模式
  16. 毕业时候写的一个PE解析小工具(MFC源码)
  17. [附源码]java毕业设计企业职工福利发放管理系统
  18. lodop打印控件工作使用总结
  19. 第四章 向量组的线性相关性
  20. SpringBoot 调用python接口

热门文章

  1. 细胞增殖曲线_【音频】第6章 细胞的生命历程 第1节 细胞的增殖
  2. 单例模式不能被继承_Spring的单例实现原理
  3. oracle update并行,Oracle update 优化方式,tuning update!
  4. opencv 直方图均等化
  5. 数据算法之二叉树删除(BinaryTreeL Remove)的Java实现
  6. freecplus框架,Linux平台下C/C++程序员提高开发效率的利器
  7. vSAN推荐的IO控制器的配置方式(转)
  8. 活动文档服务器,服务器 活动
  9. java frame的使用方法_java内部窗体internalFrame的使用方法
  10. css中英文混排 标点,浅析css中英文混排时行高不一样的原因及其解决方法