PHP的消息通信機(jī)制測試實(shí)例
本文實(shí)例講述了PHP的消息通信機(jī)制。分享給大家供大家參考,具體如下:
<?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); } } } } ?>
更多關(guān)于PHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《php curl用法總結(jié)》、《php socket用法總結(jié)》、《PHP網(wǎng)絡(luò)編程技巧總結(jié)》、《php面向?qū)ο蟪绦蛟O(shè)計(jì)入門教程》、《PHP數(shù)組(Array)操作技巧大全》、《php字符串(string)用法總結(jié)》、《php+mysql數(shù)據(jù)庫操作入門教程》及《php常見數(shù)據(jù)庫操作技巧匯總》
希望本文所述對大家PHP程序設(shè)計(jì)有所幫助。
相關(guān)文章
PHP目錄與文件操作技巧總結(jié)(創(chuàng)建,刪除,遍歷,讀寫,修改等)
這篇文章主要介紹了PHP目錄與文件操作技巧,結(jié)合實(shí)例形式總結(jié)分析了php針對文件與目錄的獲取、運(yùn)算、打開、創(chuàng)建、讀取、寫入、修改、刪除、判斷等常見操作技巧,需要的朋友可以參考下2016-09-09Laravel實(shí)現(xiàn)autoload方法詳解
本文給大家講解的是在laravel中是怎么實(shí)現(xiàn)autoload的?分析之后才發(fā)現(xiàn),真的是很巧妙,下面就來給大家詳細(xì)說明下2017-05-05php class中self,parent,this的區(qū)別以及實(shí)例介紹
我容易混淆public,private,protected,還容易混淆this,self這些東西。前面已經(jīng)寫了一篇關(guān)于public,private,protected 博文了,下面來說一下this,self,parent的用法2013-04-04php5與php7的區(qū)別點(diǎn)總結(jié)
在本篇文章里小編給大家整理的是關(guān)于php5與php7的區(qū)別是什么的相關(guān)知識點(diǎn)內(nèi)容,有需要的朋友們學(xué)習(xí)下。2019-10-10高質(zhì)量PHP代碼的50個(gè)實(shí)用技巧必備(下)
這篇文章主要為大家分享了50個(gè)高質(zhì)量PHP代碼的實(shí)用技巧,大家必備的php實(shí)用代碼,感興趣的小伙伴們可以參考一下2016-01-01