0

0

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

php中文网

php中文网

发布时间:2016-06-13 12:05:18

|

1371人浏览过

|

来源于php中文网

原创

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

转载自: php与c(或其它语言)通过消息队列进行通讯,完整代码

?

Symanto Text Insights
Symanto Text Insights

基于心理语言学分析的数据分析和用户洞察

下载

立即学习PHP免费学习笔记(深入)”;

');		}		$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免费学习笔记(深入)”;

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 #include #include #include #include #include #include #include #include #define MAX_MSG_LEN 512static 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;}

?

立即学习PHP免费学习笔记(深入)”;

运行:./m

再运行:php msg_write.php

?

立即学习PHP免费学习笔记(深入)”;

?

立即学习PHP免费学习笔记(深入)”;

?

立即学习PHP免费学习笔记(深入)”;

相关文章

PHP速学教程(入门到精通)
PHP速学教程(入门到精通)

PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载

相关标签:

php

本站声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

相关专题

更多
Word 字间距调整方法汇总
Word 字间距调整方法汇总

本专题整合了Word字间距调整方法,阅读下面的文章了解更详细操作。

2

2025.12.24

任务管理器教程
任务管理器教程

本专题整合了任务管理器相关教程,阅读下面的文章了解更多详细操作。

2

2025.12.24

AppleID格式
AppleID格式

本专题整合了AppleID相关内容,阅读专题下面的文章了解更多详细教程。

0

2025.12.24

csgo视频观看入口合集
csgo视频观看入口合集

本专题整合了csgo观看入口合集,阅读下面的文章了知道更多入口地址。

29

2025.12.24

yandex外贸入口合集
yandex外贸入口合集

本专题汇总了yandex外贸入口地址,阅读下面的文章了解更多内容。

58

2025.12.24

添加脚注通用方法
添加脚注通用方法

本专题整合了添加脚注方法合集,阅读专题下面的文章了解更多内容。

1

2025.12.24

重启电脑教程汇总
重启电脑教程汇总

本专题整合了重启电脑操作教程,阅读下面的文章了解更多详细教程。

3

2025.12.24

纸张尺寸汇总
纸张尺寸汇总

本专题整合了纸张尺寸相关内容,阅读专题下面的文章了解更多内容。

5

2025.12.24

Java Spring Boot 微服务实战
Java Spring Boot 微服务实战

本专题深入讲解 Java Spring Boot 在微服务架构中的应用,内容涵盖服务注册与发现、REST API开发、配置中心、负载均衡、熔断与限流、日志与监控。通过实际项目案例(如电商订单系统),帮助开发者掌握 从单体应用迁移到高可用微服务系统的完整流程与实战能力。

1

2025.12.24

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

精品课程

更多
相关推荐
/
热门推荐
/
最新课程
Pandas 教程
Pandas 教程

共15课时 | 0.8万人学习

PHP课程
PHP课程

共137课时 | 7.7万人学习

JavaScript ES5基础线上课程教学
JavaScript ES5基础线上课程教学

共6课时 | 6.9万人学习

关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号