PHP's message communication mechanism test instance _php skills

Source: Internet
Author: User
Tags message queue

This article describes the message communication mechanism of PHP. Share to everyone for your reference, specific as follows:

<?php error_reporting (E_all&~e_warning&~e_notice); /** * Example for sending and receiving Messages via the "System V" \ * 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 docum
 ENT is distributed into the hope that it'll 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 is send (Queue full ...)    (true/false) $msgtype _send=1; Any Integer above 0. It signeds every message.
So, 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 it to 0" you receive the "Next message" in
The Queue with any type.       $maxsize = 100;
How Long's the maximal data you are like to receive. $option _receive=msg_ipc_nowait;
             If There are no messages of the wanted type in the Queue continue without.
If is set to NULL wait for a message.
  Send or receive Messages for ($i =0; $i <20; $i + +) {sleep (1);
  Ob_flush ();
 Flush ();   $message = ' Hello, this is Flandy,now '. 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 are ". $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);

 }}}?>

For more information about PHP interested readers can view the site topics: "Php Curl Usage Summary", "PHP Socket Usage Summary", "PHP Network Programming Skills Summary", "PHP object-oriented Programming Program Introduction", "PHP Array" operation Skills Encyclopedia, " Summary of PHP string usage, Introduction to PHP+MYSQL database operations, and a summary of PHP common database operations Tips

I hope this article will help you with the PHP program design.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.