设为首页 收藏本站
查看: 720|回复: 0

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

[复制链接]

尚未签到

发表于 2017-4-10 10:15:30 | 显示全部楼层 |阅读模式
  转载自: PHP与C(或其它语言)通过消息队列进行通讯,完整代码
  

<?php
/*
* class msg
* Use for communication between php and php;
* Create at: 12:08 2012/10/31
* Author: leixun(lein_urg@163.com)
* version 1 - 14:01 2012/10/31
*/
class msg{
private $id;
private $msg_id;
private $_serialize = true;
/**
* @param $_id ID
*/
public function msg($_id, $_serialize = true){
if(!function_exists('msg_get_queue'))
{
die('msg queue function not installed, Reconfigure PHP with --enable-sysvmsg <br/>');
}
$this->id = $_id;
$this->msg_id = msg_get_queue ( $_id );
$this->_serialize = $_serialize;
if ($this->msg_id === false) {
die(basename(__FILE__).'->'.__LINE__.': Unable to create message quee');
}
}
/**
* @data data to send
* @type message type
*/
public function send( $data, $type = 1, $blocking = false )
{
if (!msg_send ($this->msg_id, $type, $data, $this->_serialize, $blocking, $msg_err))
{
return "Msg not sent because $msg_err\n";
}
return true;
}
/**
* @param $type message type
* @param $maxsize The maximum size of message to be accepted,
*/
public function receive($no_wait = true, $type = 1 , $maxsize = 1024 )
{
$rs = msg_receive ( $this->msg_id , $type ,$type , $maxsize , $message , $this->_serialize, $no_wait?MSG_IPC_NOWAIT:NULL , $errorcode);
if($rs)
return $message;
else
return false;
}
public function remove()
{
msg_remove_queue($this->msg_id);
}
}
  
  

<?php
define('base_path' , dirname(__FILE__));//msg_write.php
include(base_path.'/msg.php');
$msg = new msg(1, false);
$msg1 = new msg(2, false);
if($argv[1]=='del') $msg->remove();

$str = 'There are no user contributed notes for this page.';
while(1){
$data = substr($str,0,rand(18,25));
$msg->send(rand().$data, rand(1,10));
echo $data." -> sent\n";
echo 'Get:'.$msg1->receive(false, 0).chr(10);
sleep(3);
//usleep(10000);
}
echo 'Done';

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

#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <fcntl.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#define MAX_MSG_LEN 512
static int php_msg = -1;
static int php_msg1 = -1;
static int running = 1;
static void *php_msg_handler_thread(void *arg);
static int msg_send(int msg_id, int fd, char *data);
struct msg_st {
long mtype;
char mtext[MAX_MSG_LEN];
};
int main(int argc,char **argv)
{
printf("go 1 \n");
if((php_msg= msgget((key_t)1,0666|IPC_CREAT)) == -1)
{
perror("php_msg create");
return 0;
}
if((php_msg1= msgget((key_t)2,0666|IPC_CREAT)) == -1)
{
perror("php_msg create");
return 0;
}
/////////////////////////////////////////////////////////////////////////////////
pthread_t php_msg_pthread;
int rs = pthread_create(&php_msg_pthread, NULL, (void*(*)(void*))php_msg_handler_thread, (void *)NULL);
if(rs!=0)
{
perror("php_msg_pthread create");
return 0;
}
pthread_join(php_msg_pthread, NULL);
return 0;
}
static void *php_msg_handler_thread(void *arg)
{
struct msg_st php_data;
printf("sizeof(struct msg_st)=%d\n",sizeof(struct msg_st));
char* data;
data = malloc(MAX_MSG_LEN);
char *pre = "You told me:";
while(running){
//ssize_t msgrcv(int msqid, void *msgp, size_t msgsz, long msgtyp, int msgflg);
if(msgrcv(php_msg,(void *) &php_data, MAX_MSG_LEN, 0 , 0) == -1)
{
perror("msgrcv");
if(errno==E2BIG)
{
if(msgctl(php_msg,IPC_RMID,0) == -1)  
{  
fprintf(stderr,"msgctl(IPC_RMID) failed \n");  
}   
}
else if(errno == EINVAL)
{
sleep(1);
}
}else{
printf("recevier mssage : %s , type= %d\n", php_data.mtext, php_data.mtype);
memset(data, '\0', MAX_MSG_LEN);
memcpy(data, pre, strlen(pre));
memcpy(data+strlen(pre), php_data.mtext, strlen(php_data.mtext));
msg_send(php_msg1, 2, data);
bzero(php_data.mtext, strlen(php_data.mtext));
}
//break;
}
free(data);
}

static int msg_send(int msg_id, int fd, char *data)
{
struct msg_st some_data;
//some_data = malloc( sizeof(struct msg_st) );
memcpy(some_data.mtext, data, strlen(data) + 1);
some_data.mtext[strlen(data)] = '\0';
some_data.mtype= fd;
printf("will send %s \n", &some_data.mtext);
//int msgsnd(int msqid, const void *msgp, size_t msgsz, int msgflg);
if((msgsnd(msg_id,(void *) & some_data, strlen(data), 0)) == -1)
{
perror("msgsnd");
return 0;
}
return 1;
}


  
  
  运行:./m
  
  再运行:php msg_write.php
  

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-362775-1-1.html 上篇帖子: PHP代码网站如何防范SQL注入漏洞攻击建议 下篇帖子: 服务器php-cgi.exe进程过多,导致CPU占用100%的解决方法
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表