热转印www.yxheatpress.com

公司网站模板http://qiyemoban.software8.co/

常用链接

统计

友情链接

最新评论

PHP与C(或其它语言)通过消息队列进行通讯,完整代码

  1. <?php  
  2. /* 
  3.  * class msg 
  4.  * Use for communication between php and php; 
  5.  * Create at: 12:08 2012/10/31 
  6.  * Author: leixun(lein_urg@163.com) 
  7.  * version 1 - 14:01 2012/10/31 
  8.  */  
  9. class msg{  
  10.     private $id;  
  11.     private $msg_id;  
  12.     private $_serialize = true;  
  13.    /** 
  14.      * @param $_id ID 
  15.      */  
  16.     public function msg($_id$_serialize = true){  
  17.         if(!function_exists('msg_get_queue'))  
  18.         {  
  19.             die('msg queue function not installed, Reconfigure PHP with --enable-sysvmsg <br/>');  
  20.         }  
  21.         $this->id = $_id;  
  22.         $this->msg_id = msg_get_queue ( $_id );  
  23.         $this->_serialize = $_serialize;  
  24.         if ($this->msg_id === false) {  
  25.             die(basename(__FILE__).'->'.__LINE__.': Unable to create message quee');  
  26.         }  
  27.     }  
  28.     /** 
  29.      * @data data to send 
  30.      * @type message type 
  31.      */  
  32.     public function send( $data$type = 1, $blocking = false )  
  33.     {  
  34.         if (!msg_send ($this->msg_id, $type$data$this->_serialize, $blocking$msg_err))  
  35.         {  
  36.             return "Msg not sent because $msg_err\n";  
  37.         }
  38.         return true;  
  39.     }  
  40.   
  41.     /** 
  42.      * @param $type message type 
  43.      * @param $maxsize The maximum size of message to be accepted, 
  44.      */  
  45.     public function receive($no_wait = true, $type = 1 , $maxsize = 1024 )  
  46.     {  
  47.         $rs = msg_receive ( $this->msg_id , $type ,  $type , $maxsize , $message , $this->_serialize, $no_wait?MSG_IPC_NOWAIT:NULL , $errorcode);  
  48.         if($rs)  
  49.             return $message;  
  50.         else  
  51.             return false;  
  52.     }  
  53.   
  54.     public function remove()  
  55.     {  
  56.         msg_remove_queue($this->msg_id);  
  57.     }     
  58. }  
  1. <?php  
  2. define('base_path' , dirname(__FILE__));//msg_write.php  
  3. include(base_path.'/msg.php');  
  4. $msg = new msg(1, false);  
  5. $msg1 = new msg(2, false);  
  6. if($argv[1]=='del') $msg->remove();  
  7.    
  8.    
  9. $str = 'There are no user contributed notes for this page.';  
  10. while(1){  
  11.         $data = substr($str,0,rand(18,25));  
  12.         $msg->send(rand().$data, rand(1,10));  
  13.         echo $data." -> sent\n";  
  14.         echo 'Get:'.$msg1->receive(false, 0).chr(10);  
  15.         sleep(3);  
  16.       //usleep(10000);  
  17. }  
  18. echo 'Done';  

C, gcc -g -o m msg.c -lpthread;

  1. #include <stdio.h>  
  2. #include <errno.h>  
  3.   
  4. #include <stdlib.h>  
  5. #include <fcntl.h>  
  6. #include <string.h>  
  7. #include <unistd.h>  
  8. #include <sys/types.h>  
  9. #include <sys/ipc.h>  
  10. #include <sys/msg.h>  
  11. #define MAX_MSG_LEN 512  
  12. static int php_msg = -1;  
  13. static int php_msg1 = -1;  
  14. static int running = 1;  
  15. static void *php_msg_handler_thread(void *arg);  
  16. static int msg_send(int msg_id, int fd, char *data);   
  17. struct msg_st {  
  18.     long mtype;  
  19.     char mtext[MAX_MSG_LEN];  
  20. };    
  21. void logst(struct msg_st some_data);  
  22. int main(int argc,char **argv)  
  23. {  
  24.     printf("go 1 \n");  
  25.     if((php_msg= msgget((key_t)1,0666|IPC_CREAT)) == -1)  
  26.     {  
  27.         perror("php_msg create");  
  28.         return 0;  
  29.     }   
  30.     if((php_msg1= msgget((key_t)2,0666|IPC_CREAT)) == -1)  
  31.     {  
  32.         perror("php_msg create");  
  33.         return 0;  
  34.     }  
  35.     /////////////////////////////////////////////////////////////////////////////////  
  36.     pthread_t php_msg_pthread;  
  37.     int rs = pthread_create(&php_msg_pthread, NULL, (void*(*)(void*))php_msg_handler_thread, (void *)NULL);  
  38.    if(rs!=0)  
  39.     {  
  40.         perror("php_msg_pthread create");  
  41.         return 0;  
  42.     }  
  43.     pthread_join(php_msg_pthread, NULL);  
  44.     return 0;  
  45. }   
  46. static void *php_msg_handler_thread(void *arg)  
  47. {  
  48.     struct msg_st php_data;       
  49.     printf("sizeof(struct msg_st)=%d\n",sizeof(struct msg_st));  
  50.     int msg_to_recevie = 0;  
  51.     char* data;  
  52.     data = malloc(MAX_MSG_LEN);  
  53.     char *pre = "You told me:";   
  54.     while(running){  
  55.         if(msgrcv(php_msg,(void *) &php_data, MAX_MSG_LEN, msg_to_recevie , 0) == -1)  
  56.         {  
  57.             perror("msgrcv");  
  58.             if(errno==E2BIG)  
  59.             {                 
  60.                 if(msgctl(php_msg,IPC_RMID,0) == -1)    
  61.                 {    
  62.                     fprintf(stderr,"msgctl(IPC_RMID) failed \n");    
  63.                 }     
  64.             }  
  65.             else if(errno == EINVAL)  
  66.             {  
  67.                 sleep(1);  
  68.             }  
  69.         }else{        
  70.             printf("recevier mssage : %s , type= %d, msg_to_recevie= %d;\n", php_data.mtext, php_data.mtype,msg_to_recevie);  
  71.             memset(data, '\0', MAX_MSG_LEN);  
  72.             memcpy(data, pre, strlen(pre));  
  73.             memcpy(data+strlen(pre), php_data.mtext, strlen(php_data.mtext));  
  74.             msg_send(php_msg1, 2, data);      
  75.             bzero(php_data.mtext, strlen(php_data.mtext));  
  76.         }  
  77.         //break;  
  78.     }  
  79.     free(data);  
  80. }   
  81. static int msg_send(int msg_id, int fd, char *data)  
  82. {  
  83.     struct msg_st* some_data;  
  84.     some_data = malloc( sizeof(struct msg_st) );  
  85.     memcpy(some_data->mtext, data, strlen(data) + 1);  
  86.     some_data->mtext[strlen(data)] = '\0';  
  87.     some_data->mtype= fd;  
  88.     printf("will send %s \n", &some_data->mtext);  
  89.     if((msgsnd(msg_id,(void *) some_data, MAX_MSG_LEN,0)) == -1)              
  90.     {  
  91.         perror("msgsnd");  
  92.         return 0;  
  93.     }  
  94.     return 1;  
  95. }    
  96. void logst(struct msg_st some_data)  
  97. {  
  98.     FILE *fp;  
  99.     fp = fopen("file.dat", "w+");     
  100.     if (!fp)    
  101.    {    
  102.        printf("open file error!");    
  103.         return;  
  104.     }   
  105.     fwrite(&some_data, sizeof(struct msg_st), 1, fp);    
  106.     fclose(fp);   
  107. }  
运行:./m
再运行:php msg_write.php
原文:http://www.software8.co/wzjs/cpp/1002.html

posted on 2012-11-10 10:17 不听话的 阅读(1417) 评论(0)  编辑 收藏 引用


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理