Hi there,I AM STEVE.
Well I need to write a program to realize the inter-process communication with message quque, but when I finished it, the program when running seems to be blocked or just cann't send message to the user. the code like this:
server:
Code:
#include"unistd.h"
#include"stdio.h"
#include"stdlib.h"
#include"sys/ipc.h"
#include"string.h"
#include"sys/msg.h"
#include"sys/stat.h"
#include"errno.h"

#define TEXT_SIZE 255

struct msg_buf{
	long type;
	char text[TEXT_SIZE+1];
};

int main()
{
	key_t key;
	int msg_id;
	struct msg_buf msg;

//=============================================================================
	if(key=ftok(".",'a')==-1){
		perror("Create key error.");
		exit(1);
	}
	printf("key=%d\n",key);

	if(msg_id=msgget(key,IPC_CREAT)==-1){
		perror("Create message error.");
		exit(1);
	}
	printf("msg_id=%d\n",msg_id);
//=============================================================================
	while(1){

		if(msgrcv(msg_id,&msg,sizeof(struct msg_buf),1, 0)!=-1){
			printf("REC:%s from typ: %d\n",msg.text,msg.type);
		}

		int cmpret=strncmp(msg.text,"quit",4);
		msg.type=8888;
		
		printf("\nSend>");
		fgets(msg.text,sizeof(msg.text),stdin);
	//  strcpy(msg.text,"Ok!\n");
		msgsnd(msg_id,&msg,sizeof(struct msg_buf),0);
		
	//	printf("cmpret=%d\n",cmpret);
		if(cmpret==0){
			break;
		}
		}
		msgctl(msg_id,IPC_RMID,NULL);
		exit(0);
}
client:
Code:
#define TEXT_SIZE 255

struct msg_buf{
	long type;
	char text[TEXT_SIZE+1];
};

int main(int argc,char **argv)
{
	key_t key;
	int	msg_id;
	struct msg_buf msg;
	
//	if(argc!=2){
//		fprintf(stderr,"Usage:%s'your message string'\n",argv[0]);
//		exit(1);
//	}
//=============================================================================
	if(key=ftok(".",'a')==-1){
		perror("Create key error.");
		exit(1);
	}
	printf("key=%d\n",key);

	if(msg_id=msgget(key,IPC_CREAT)==-1){
		perror("Create message error.");
		exit(1);
	}
	printf("msg_id=%d\n",msg_id);
//=============================================================================
		msg.type=1;
	//	strncpy(msg.text,argv[1],TEXT_SIZE);
	    printf("\nSend>");
		fgets(msg.text,sizeof(msg.text),stdin);
		msgsnd(msg_id,&msg,sizeof(struct msg_buf),0);

		memset(&msg,'\0',sizeof(struct msg_buf));
		msgrcv(msg_id,&msg,sizeof(struct msg_buf),8888, 0);
		fprintf(stdout,"Client recieve:%s,type:%d\n",msg.text,msg.type);
		exit(0);
}
Now they can't communicate with each other.Why?Thanks for your time.