msgclient.c

来自「基于linux下的故障录波前置程序」· C语言 代码 · 共 75 行

C
75
字号
#include<stdlib.h>
#include<stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <errno.h>
#include<signal.h>
#include<sys/ipc.h>
#include<sys/msg.h>
#include<error.h>


//int   MsgSnd ( MsgQid , MsgContent , MsgKey , MsgLen )
//int   MsgQid       ;
//char  *MsgContent  ;
//long  MsgKey       ;
//int   MsgLen       ;
//{
//   struct
//   {
//      long  pMsgKey                     ;
//      char  pMsgContent[ MAX_QUE_PACKSIZE ] ;
//   } Message                             ;
//
//   int   SndLen                          ;
//
//   Message.pMsgKey = MsgKey ;
//   memset ( Message.pMsgContent , NULL , MAX_QUE_PACKSIZE ) ;
//   memcpy ( Message.pMsgContent , MsgContent , MsgLen ) ;
//
//   SndLen = msgsnd ( (int)MsgQid , (const void *)&Message , (unsigned long)MsgLen , (int)IPC_NOWAIT ) ;
//
//   if ( SndLen < 0 )
//      return ERROR   ;
//   else
//      return SUCCESS ;
//}

typedef struct _Msg
{
	long type;
	char buffer[1024];
}Msg;

int main()
{
	key_t key;
	int msgid;
	Msg msg;
	int RcvLen;
	
	key = ftok("/root/aa",'Y');
	
	msgid = msgget(key,IPC_CREAT|0666);
	
	if(msgid<0 )
	{
			perror("open msg error:");
			return -1;
	
	}
	while(1)
	{
		RcvLen = msgrcv ( (int)msgid , (void *)&msg , 1024 , 0 , 0 ) ;

   if ( RcvLen <= 0 ) 
       continue ;
   else
   {
      printf("%s\n",msg.buffer);
   
   }
	}
	return 0;
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?