查阅资料,发现c也有消息队列函数,令我很兴奋,找了一个c的列子,c本身消息队列收发成功了。然后我尝试php和c对发,因为c发送的的是c的结构体
struct,所以我寻求php生成c结构体struct的办法,我用了pack,搜索了pack,或者php
struct,出来的文章都是同一篇,完全不正确的,经过记录c发送的数据,然后将我用php
pack的数据做对比,发现数据是一模一样的,但是,发送给c,c无法解析。还好我有点儿php扩展基础,找到php的消息函数,一看:我失望了,php
的消息函数发送的struct是固定的,一个int和一个char[1],经过一番测试之后,能将一个数字和一个字符串发送给c了
PHP代码:
1
2 <?php
3 $id = msg_get_queue ( 1 );
4 if (!msg_send ($id, 317, "sdsadsdsds", false, true, $msg_err))
5 {
6
7 echo "faile!";
8 return "Msg not sent because $msg_err\n";
9 }else{
10 echo "success!";
11 }
12
13 ?>
C代码:
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <fcntl.h>
4 #include <string.h>
5 #include <unistd.h>
6 #include <sys/types.h>
7 #include <sys/ipc.h>
8 #include <sys/msg.h>
9 #define MAX_TEXT 512
10 #define BUFSIZE BUFSIZ
11
12 struct msg_st {
13 long mtype;
14 char mtext[1];
15 };
16
17 void logst(struct msg_st some_data);
18
19 int main(int argc,char **argv)
20 {
21 while(1){
22
23 int msgid1;
24 struct msg_st some_data1;
25 int msg_to_recevie = 0;
26 if((msgid1= msgget((key_t)1,0666|IPC_CREAT)) == -1)
27 {
28 perror("msgget");
29 exit(EXIT_FAILURE);
30 }
31 if(msgrcv(msgid1,(void *) &some_data1, BUFSIZ, msg_to_recevie , 0) == -1)
32 {
33 perror("msgrcv");
34 exit(EXIT_FAILURE);
35 }
36 printf("recevier mssage : %s, type= %d;\n", some_data1.mtext, some_data1.mtype);
37 //printf("%s, %d\n", msg_text, strlen(msg_text));
38
39 if(msgctl(msgid1,IPC_RMID,0) == -1)
40 {
41 fprintf(stderr,"msgctl(IPC_RMID) failed \n");
42 exit(EXIT_FAILURE);
43 }
44 //break;
45 sleep(1);
46 }
47
48 }
参考链接:
http://blog.csdn.net/leinchu/article/details/8132530
http://blog.csdn.net/guoping16/article/details/6584024