PHP的消息通信机制测试实例
本文实例讲述了PHP的消息通信机制。分享给大家供大家参考,具体如下:
<?phperror_reporting(E_ALL&~E_WARNING&~E_NOTICE);
/**
*ExampleforsendingandreceivingMessagesviatheSystemVMessageQueue
*
*Totrythisscriptrunitsynchron/asynchrontwicetimes.Onetimewith?type=sendandonetimewith?type=receive
*
*@authorThomasEimers-MehrkanalGmbH
*
*Thisdocumentisdistributedinthehopethatitwillbeuseful,butwithoutanywarranty;
*withouteventheimpliedwarrantyofmerchantabilityorfitnessforaparticularpurpose.
*/
ob_implicit_flush(1);
header('Content-Type:text/plain;charset=ISO-8859-1');
echo"Start...\n";
//CreateSystemVMessageQueue.IntegervalueisthenumberoftheQueue
//$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;//Mustthetransferdatabeserialized?
$block_send=false;//BlockifMessagecouldnotbesend(Queuefull...)(true/false)
$msgtype_send=1;//AnyIntegerabove0.ItsignedseveryMessage.Soyoucouldhandlemultiblemessage
//typeinoneQueue.
//Receiveoptions
$msgtype_receive=1;//WhichetypeofMessagewewanttoreceive?(Here,thetypeisthesameasthetypewesend,
//butifyousetthisto0youreceivethenextMessageintheQueuewithanytype.
$maxsize=100;//Howlongisthemaximaldatayouliketoreceive.
$option_receive=MSG_IPC_NOWAIT;//IftherearenomessagesofthewantedtypeintheQueuecontinuewithoutwating.
//IfissettoNULLwaitforaMessage.
//Sendorreceive20Messages
for($i=0;$i<20;$i++){
sleep(1);
ob_flush();
flush();
$message='Hello,ThisisFlandy,nowis'.date("H:i:s",time());//TransferingData
//Thisonesends
if(isset($_GET['type'])&&$_GET['type']=='send'){
if(msg_send($queue,$msgtype_send,$message,$serialize_needed,$block_send,$err)===true){
echo"The".$i."Messagehasbeensent,themessgeis".$message."\n";
}else{
var_dump($err);
}
//Thisonereceived
}else{
$queue_status=msg_stat_queue($queue);
echo'GetMessagesinthequeue:'.$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"Receiveddata:".$daten."\n";
}else{
var_dump($err);
}
}
}
}
?>
更多关于PHP相关内容感兴趣的读者可查看本站专题:《phpcurl用法总结》、《phpsocket用法总结》、《PHP网络编程技巧总结》、《php面向对象程序设计入门教程》、《PHP数组(Array)操作技巧大全》、《php字符串(string)用法总结》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总》
希望本文所述对大家PHP程序设计有所帮助。