PHP的消息通信機(jī)制測(cè)試實(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ù)庫(kù)操作入門教程》及《php常見(jiàn)數(shù)據(jù)庫(kù)操作技巧匯總》
希望本文所述對(duì)大家PHP程序設(shè)計(jì)有所幫助。
相關(guān)文章
PHP目錄與文件操作技巧總結(jié)(創(chuàng)建,刪除,遍歷,讀寫(xiě),修改等)
這篇文章主要介紹了PHP目錄與文件操作技巧,結(jié)合實(shí)例形式總結(jié)分析了php針對(duì)文件與目錄的獲取、運(yùn)算、打開(kāi)、創(chuàng)建、讀取、寫(xiě)入、修改、刪除、判斷等常見(jiàn)操作技巧,需要的朋友可以參考下2016-09-09
PHP浮點(diǎn)數(shù)的一個(gè)常見(jiàn)問(wèn)題
本文主要給大家詳細(xì)介紹了下php的浮點(diǎn)數(shù),以及在應(yīng)用中關(guān)于浮點(diǎn)數(shù)的一個(gè)小問(wèn)題,有需要的小伙伴可以參考下2016-03-03
Laravel實(shí)現(xiàn)autoload方法詳解
本文給大家講解的是在laravel中是怎么實(shí)現(xiàn)autoload的?分析之后才發(fā)現(xiàn),真的是很巧妙,下面就來(lái)給大家詳細(xì)說(shuō)明下2017-05-05
PHP 5.3新特性命名空間規(guī)則解析及高級(jí)功能
本文介紹了PHP命名空間的一些術(shù)語(yǔ),其解析規(guī)則,以及一些高級(jí)功能的應(yīng)用,希望能夠幫助讀者在項(xiàng)目中真正使用命名空間。2010-03-03
php通過(guò)strpos查找字符串出現(xiàn)位置的方法
這篇文章主要介紹了php通過(guò)strpos查找字符串出現(xiàn)位置的方法,實(shí)例分析了strpos的功能及使用技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-03-03
php class中self,parent,this的區(qū)別以及實(shí)例介紹
我容易混淆public,private,protected,還容易混淆this,self這些東西。前面已經(jīng)寫(xiě)了一篇關(guān)于public,private,protected 博文了,下面來(lái)說(shuō)一下this,self,parent的用法2013-04-04
php5與php7的區(qū)別點(diǎn)總結(jié)
在本篇文章里小編給大家整理的是關(guān)于php5與php7的區(qū)別是什么的相關(guān)知識(shí)點(diǎn)內(nèi)容,有需要的朋友們學(xué)習(xí)下。2019-10-10
高質(zhì)量PHP代碼的50個(gè)實(shí)用技巧必備(下)
這篇文章主要為大家分享了50個(gè)高質(zhì)量PHP代碼的實(shí)用技巧,大家必備的php實(shí)用代碼,感興趣的小伙伴們可以參考一下2016-01-01

