/*接收消息--单项的接收消息*/
来源:互联网 发布:大淘客cms怎么样 编辑:程序博客网 时间:2024/05/17 03:26
/*接收消息--单项的接收消息*/
#include<stdlib.h>
#include<stdio.h>
#include<string.h>
#include<unistd.h>
#include<sys/types.h>
#include<sys/ipc.h>
#include<sys/msg.h> //头文件信息
// 系统定义的BUFSIZ的值为8192
struct my_msg_st //定义一个结构体名为my_msg_st
{
int my_msg_type; //结构体成员,定义一个名为my_msg_type为整型数据
char msg_text[BUFSIZ]; //结构本成员,定义一个字符数组类型my_text[512]
};
int main(int argc, char *argv[]) //主函数,包含两个参数
{
int running=1; //定义running为整型数据,给running赋初值为1
int msgid; //定义msgid为整型数据
struct my_msg_st some_data; //定义了一个结构体变量名为some_data
int msg_to_receive=0; //整型msg_to_reseive赋初值为0
/*下面:如果可要创建一个Key值为12345,且户权限为0666的消息队列,如果创建失败为-1,则执行下面*/
if((msgid=msgget((key_t)12345,0666|IPC_CREAT))==-1)
{
perror("msgget"); //当msgid等于-1时则打印错误的信息
exit(EXIT_FAILURE); //异常退出
}
while(running) //当running的值为"真"时
{
/*下面:(msg_to_receive优先权)接收最大为BUFSIZ大的结构体变量some_data的消息队列msgid*/
if(msgrcv(msgid,(void *)&some_data,BUFSIZ,msg_to_receive,0)==-1)
{
perror("msgrcv"); //打印错误的信息
exit(EXIT_FAILURE); //异常退出
}
printf("receiver mssage:%s",some_data.msg_text); //打印接受到的字符串
if(strncmp(some_data.msg_text,"end",3)==0) //当end这3个字符复制到buffer中时,执行下面
running=0; //把零赋给running.
}
if(msgctl(msgid,IPC_RMID,0)==-1) //从系统中删除msgid这个消息队列,
{
fprintf(stderr,"msgctl(IPC_RMID)failed/n"); //打印错误的信息
exit(EXIT_FAILURE); //异常退出
}
return 0; //返回零
}
- /*接收消息--单项的接收消息*/
- 消息队列的接收
- 消息接收
- 接收消息
- 接收消息
- 接收来自MQ的消息
- 窗口过程接收的消息
- UILabel的接收点击消息
- Fragment的广播消息接收
- ActiveMQ的消息接收确认
- 消息的接收和处理
- gloox接收消息
- gloox接收消息
- gloox 接收消息
- gloox接收消息
- windows接收登陆消息
- gloox接收消息
- NUGI poplist接收消息
- [分享] Linux下用Anjuta写个Hello World 的C++程序竟如此简单!
- Windows API一日一练(47)CreateSemaphore和ReleaseSemaphore函数
- Web 测试
- Eclipse环境配置
- 基于S3C2410平台的LCD for Linux 2.6 驱动移植
- /*接收消息--单项的接收消息*/
- JDBC基础1
- 耻民
- 领域模型﹐打开OO的另一扇窗
- 类的const和非const成员函数的重载
- 接口
- JavaScript的学习笔记-3
- Microsoft Windows® 2000的加密服务提供程序(CSP, Cryptographic Service Provider)
- WORD实用奇招妙技大荟萃