• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            熱轉印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 不聽話的 閱讀(1426) 評論(0)  編輯 收藏 引用

            亚洲综合精品香蕉久久网97| 国内精品久久久久影院优| 精品久久久久久99人妻| 热99re久久国超精品首页| 九九久久精品无码专区| 一本一道久久a久久精品综合 | 久久国产精品一区二区| 手机看片久久高清国产日韩| 久久婷婷五月综合国产尤物app | 99久久国产综合精品女同图片| 久久66热人妻偷产精品9| 狠狠色伊人久久精品综合网| 久久精品中文字幕大胸| 青青青国产成人久久111网站| 午夜精品久久影院蜜桃| 久久99免费视频| 无码人妻少妇久久中文字幕蜜桃| 久久久久久久综合日本| 久久精品人人做人人爽电影| 欧美国产成人久久精品| 日本久久久久久久久久| 精品国产热久久久福利| 国产精品久久精品| 久久99精品久久久久子伦| 久久久久av无码免费网| 热RE99久久精品国产66热| 国产91久久综合| 99久久精品九九亚洲精品| 国产精品久久久久久| 色欲av伊人久久大香线蕉影院| 欧美与黑人午夜性猛交久久久| 色综合色天天久久婷婷基地| 99精品国产在热久久无毒不卡| 亚洲欧美日韩中文久久| 亚洲精品乱码久久久久久蜜桃图片| 亚洲国产成人乱码精品女人久久久不卡 | 久久伊人精品青青草原日本| 国产精品免费看久久久香蕉| 2020最新久久久视精品爱 | 久久福利片| 香蕉久久夜色精品国产2020|