Thread: Need help to do the big task

  1. #1
    Registered User
    Join Date
    Jun 2010
    Posts
    27

    Need help to do the big task

    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.

  2. #2
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    The client/server code looks good, and the problem is of a more mundane nature
    Code:
    if (msg_id=msgget(key,IPC_CREAT)==-1) /* which has higher precedence - the equality or assignment operator? */

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How Big is Too Big
    By kpreston in forum C Programming
    Replies: 4
    Last Post: 10-25-2008, 10:16 AM
  2. Replies: 2
    Last Post: 12-31-2007, 11:40 AM
  3. Where do a task get "wakeuped", blocked, preempted?
    By micke_b in forum Linux Programming
    Replies: 4
    Last Post: 12-28-2007, 04:49 PM
  4. A Task Buffer for storing socket descriptors
    By cloudy in forum Networking/Device Communication
    Replies: 0
    Last Post: 09-09-2006, 01:08 PM
  5. Big and little endian
    By Cactus_Hugger in forum C Programming
    Replies: 4
    Last Post: 10-12-2005, 07:07 PM