阅读背景:

linux网络编程之System V 消息队列:消息队列实现回射客户/服务器和 msgsnd、msgrcv 函数

来源:互联网 
#include<stdlib.h>
#include <stdio.h>
#include<sys/ipc.h>
#include<sys/msg.h>
#include<sys/types.h>
#include<unistd.h>
#include<errno.h>
#include<string.h>

#define ERR_EXIT(m) \
    do { \
        perror(m); \
        exit(EXIT_FAILURE); \
    } while(0)

#define MSGMAX 8192
	    
struct mymsgbuf
{
	long mtype;
	char mtext[MSGMAX];
};


void echo_ser(int msgid)
{
	struct mymsgbuf msg;
	memset(&msg, 0, sizeof(msg));
	int nrcv = 0;
	while (1)
	{

		if ((nrcv = msgrcv(msgid, &msg, MSGMAX, 1, 0)) < 0)
			;
		int pid = *((int *)msg.mtext);
		fputs(msg.mtext + 4, stdout);
		msg.mtype = pid;
		msgsnd(msgid, &msg, nrcv, 0);
		memset(&msg, 0, sizeof(msg));

	}
}

int main(int argc, char *argv [])
{
	int msgid;
	msgid = msgget(1234, IPC_CREAT | 0666);
	if (msgid == -1)
		ERR_EXIT("msgget");

	echo_ser(msgid);


	return 0;
}#include<stdlib.h>
#include <stdio.h>
#include<



你的当前访问异常,请进行认证后继续阅读剩余内容。

分享到: