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

[php]  view plain copy
  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.   
  10. class msg{  
  11.     private $id;  
  12.     private $msg_id;  
  13.     private $_serialize = true;  
  14.   
  15.     /** 
  16.      * @param $_id ID 
  17.      */  
  18.     public function msg($_id$_serialize = true){  
  19.         if(!function_exists('msg_get_queue'))  
  20.         {  
  21.             die('msg queue function not installed, Reconfigure PHP with --enable-sysvmsg <br/>');  
  22.         }  
  23.         $this->id = $_id;  
  24.         $this->msg_id = msg_get_queue ( $_id );  
  25.         $this->_serialize = $_serialize;  
  26.         if ($this->msg_id === false) {  
  27.             die(basename(__FILE__).'->'.__LINE__.': Unable to create message quee');  
  28.         }  
  29.     }  
  30.   
  31.     /** 
  32.      * @data data to send 
  33.      * @type message type 
  34.      */  
  35.     public function send( $data$type = 1, $blocking = false )  
  36.     {  
  37.         if (!msg_send ($this->msg_id, $type$data$this->_serialize, $blocking$msg_err))  
  38.         {  
  39.             return "Msg not sent because $msg_err\n";  
  40.         }  
  41.         return true;  
  42.     }  
  43.   
  44.     /** 
  45.      * @param $type message type 
  46.      * @param $maxsize The maximum size of message to be accepted, 
  47.      */  
  48.     public function receive($no_wait = true, $type = 1 , $maxsize = 1024 )  
  49.     {  
  50.         $rs = msg_receive ( $this->msg_id , $type ,  $type , $maxsize , $message , $this->_serialize, $no_wait?MSG_IPC_NOWAIT:NULL , $errorcode);  
  51.         if($rs)  
  52.             return $message;  
  53.         else  
  54.             return false;  
  55.     }  
  56.   
  57.     public function remove()  
  58.     {  
  59.         msg_remove_queue($this->msg_id);  
  60.     }     
  61. }  

 

[php]  view plain copy
  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;

[cpp]  view plain copy
  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.   
  15. static int running = 1;  
  16.   
  17. static void *php_msg_handler_thread(void *arg);  
  18. static int msg_send(int msg_id, int fd, char *data);  
  19.   
  20. struct msg_st {  
  21.     long mtype;  
  22.     char mtext[MAX_MSG_LEN];  
  23. };  
  24.   
  25. int main(int argc,char **argv)  
  26. {  
  27.     printf("go 1 \n");  
  28.   
  29.     if((php_msg= msgget((key_t)1,0666|IPC_CREAT)) == -1)  
  30.     {  
  31.         perror("php_msg create");  
  32.         return 0;  
  33.     }  
  34.       
  35.     if((php_msg1= msgget((key_t)2,0666|IPC_CREAT)) == -1)  
  36.     {  
  37.         perror("php_msg create");  
  38.         return 0;  
  39.     }  
  40.   
  41.     /  
  42.     pthread_t php_msg_pthread;  
  43.   
  44.     int rs = pthread_create(&php_msg_pthread, NULL, (void*(*)(void*))php_msg_handler_thread, (void *)NULL);  
  45.     if(rs!=0)  
  46.     {  
  47.         perror("php_msg_pthread create");  
  48.         return 0;  
  49.     }  
  50.   
  51.     pthread_join(php_msg_pthread, NULL);  
  52.     return 0;  
  53.   
  54. }  
  55.   
  56. static void *php_msg_handler_thread(void *arg)  
  57. {  
  58.     struct msg_st php_data;       
  59.     printf("sizeof(struct msg_st)=%d\n",sizeof(struct msg_st));  
  60.   
  61.     char* data;  
  62.     data = malloc(MAX_MSG_LEN);  
  63.     char *pre = "You told me:";   
  64.     while(running){  
  65.         //ssize_t msgrcv(int msqid, void *msgp, size_t msgsz, long msgtyp, int msgflg);   
  66.         if(msgrcv(php_msg,(void *) &php_data, MAX_MSG_LEN, 0 , 0) == -1)  
  67.         {  
  68.             perror("msgrcv");  
  69.             if(errno==E2BIG)  
  70.             {                 
  71.                 if(msgctl(php_msg,IPC_RMID,0) == -1)    
  72.                 {    
  73.                     fprintf(stderr,"msgctl(IPC_RMID) failed \n");    
  74.                 }     
  75.             }  
  76.             else if(errno == EINVAL)  
  77.             {  
  78.                 sleep(1);  
  79.             }  
  80.         }else{        
  81.             printf("recevier mssage : %s , type= %d\n", php_data.mtext, php_data.mtype);  
  82.             memset(data, '\0', MAX_MSG_LEN);  
  83.             memcpy(data, pre, strlen(pre));  
  84.   
  85.             memcpy(data+strlen(pre), php_data.mtext, strlen(php_data.mtext));  
  86.             msg_send(php_msg1, 2, data);  
  87.               
  88.             bzero(php_data.mtext, strlen(php_data.mtext));  
  89.         }  
  90.         //break;  
  91.     }  
  92.     free(data);  
  93. }  
  94.   
  95.   
  96. static int msg_send(int msg_id, int fd, char *data)  
  97. {  
  98.     struct msg_st some_data;  
  99.   
  100.     //some_data = malloc( sizeof(struct msg_st) );  
  101.   
  102.     memcpy(some_data.mtext, data, strlen(data) + 1);  
  103.     some_data.mtext[strlen(data)] = '\0';  
  104.     some_data.mtype= fd;  
  105.   
  106.     printf("will send %s \n", &some_data.mtext);  
  107.     //int msgsnd(int msqid, const void *msgp, size_t msgsz, int msgflg);   
  108.     if((msgsnd(msg_id,(void *) & some_data, strlen(data), 0)) == -1)              
  109.     {  
  110.         perror("msgsnd");  
  111.         return 0;  
  112.     }  
  113.   
  114.     return 1;  
  115. }  

 

运行:./m

再运行:php msg_write.php


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个简单的 C 语言消息队列的程序代码,包含了消息队列的创建、发送和接收功能: ```c #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <sys/types.h> #include <sys/ipc.h> #include <sys/msg.h> #include <string.h> #define MSG_KEY 1234 // 定义消息结构体 struct msgbuf { long mtype; // 消息类型 char mtext[256]; // 消息正文 }; int main() { int msgid, ret; struct msgbuf msg; // 创建消息队列 msgid = msgget(MSG_KEY, IPC_CREAT | 0666); if (msgid < 0) { perror("msgget"); return -1; } // 发送消息 msg.mtype = 1; strcpy(msg.mtext, "Hello, world!"); ret = msgsnd(msgid, &msg, sizeof(msg.mtext), 0); if (ret < 0) { perror("msgsnd"); return -1; } // 接收消息 ret = msgrcv(msgid, &msg, sizeof(msg.mtext), 1, 0); if (ret < 0) { perror("msgrcv"); return -1; } printf("Received message: %s\n", msg.mtext); // 删除消息队列 ret = msgctl(msgid, IPC_RMID, NULL); if (ret < 0) { perror("msgctl"); return -1; } return 0; } ``` 在该程序中,我们先定义了一个消息结构体 `msgbuf`,其中包含了消息的类型和正文。程序接着创建了一个消息队列,使用 `msgget` 函数,该函数返回一个消息队列的标识符 `msgid`,以后就可以用这个标识符进行消息队列的操作。 接着,程序通过 `msgsnd` 函数向消息队列中发送了一个消息。该函数的第一个参数是消息队列的标识符,第二个参数是指向消息结构体的指针,第三个参数是消息正文的长度,第四个参数是一个标志,一般设置为 0。 最后,程序通过 `msgrcv` 函数从消息队列中接收了一个消息。该函数的第一个参数是消息队列的标识符,第二个参数是指向消息结构体的指针,第三个参数是消息正文的长度,第四个参数是消息类型,表示要接收的消息类型。如果设置为 0,则表示接收队列中第一个消息。第五个参数是标志,一般设置为 0。 最后,程序通过 `msgctl` 函数删除了创建的消息队列。该函数的第一个参数是消息队列的标识符,第二个参数是删除命令,一般为 IPC_RMID,表示删除该消息队列。第三个参数是一个指向结构体的指针,一般设置为 NULL。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值