Client-server program

This is a discussion on Client-server program within the C Programming forums, part of the General Programming Boards category; Gday everyone, I'd like to display recent received messages in the buffer 'msg_cat' when user types in 'display', but it ...

  1. #1
    Registered User
    Join Date
    Sep 2006
    Posts
    5

    Client-server program

    Gday everyone,

    I'd like to display recent received messages in the buffer 'msg_cat' when user types in 'display', but it always displays 'No recent messages' after I concatenated messages into 'msg_cat'. I use global buffer 'msg_cat'. Is there something wrong of the way I use 'msg_cat'? Or the affect of using fork function in main?

    I spent so much time on time, but I really don't know how to fix this problem.

    It really depends on you guys experts here. Please help me out if you're interested in this.

    I'd appreciate it.



    Code:
    char msg_cat[1000];	
    
    //Receive, store and display recent messages when user type 'display'.
    void displayMulticastedMessage(int condition){
    	char *msg;	
    	int stop = -1;	
          
    
            //Display recent multicast messages.
    	if (condition == 2){
    		if (strcmp(msg_cat, "") != 0){
    			printf("msg_cat: %s", msg_cat);
    			strcpy(msg_cat, "");
    		}
    		else{
    			printf("No recent messages\n");
    		}
    	}
    
    	while(stop < 0){
    		//Receive multicasted messages from server and store in msg buffer.
    		msg = (char *)recvfromDST (&read_EP, sockMulti);	
    
    		//Concatenate message msg into buffer msg_cat.
    		strcat(msg_cat, msg);
    		strcat(msg_cat, "\n");					
    	
    		//Display a receiving multicast message to user
    		if (msg != NULL){
    			printf("----- Waiting for Multicasted message ----- \n");
    			printf("\nReceived multicast message ==> %s \n", msg);
    		}
    		else{
    			stop = 0;
    		}			
    	}
    }
    
    
    
    void receiveUserMessage(int sock){		
    	int stop = -1;
    	char *msg;	
    
    	while (stop < 0){
    		printf("Type message or 'display' to display recent multicasted messages or	'end' to quit \n");	
    		
    		//Read msg from user
    	  		scanf ("%s", KB_buff);		
    		//Display recent multicast messages.
    		if (strncmp(KB_buff, "display", 7) == 0){	
    			int display = 2;
    			displayMulticastedMessage(display);
    		}	
    		
    		//Check message format and send message if it is correct.
    		else{			
    				int stop = sentMessage(KB_buff,sock);			
    		}		
    		printf("\n");
    	}
    }
    
    
    
    int main(){
    
    if (pid == 0) // i am the child process
    		{
    			
    			//Receive user input and commands
    			receiveUserMessage(MC_socket);
    		}
    		else 
    		{
    			if (pid < 0) // ERROR !
    			{
    				perror("fork");
    				exit(1);
    			}
    			else // parent process
    			{				
    				int user = -1;
    				displayMulticastedMessage(user);	
    						
    			}
    		}
    }

  2. #2
    Fountain of knowledge.
    Join Date
    May 2006
    Posts
    794
    Have you tried debugging it?

  3. #3
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    Where are you calling fork()?

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    5
    I didn't put the code for declaration of fork to make it easy to read.Sorry

    Ofcourse I tried to run it for ages. I tried to print out the buffer 'msg_cat', then it works.
    But when I call it from function 'receiveUserMessage', then it prints nothing.

    Here is the code.

    Code:
    char msg_cat[1000];	
    
    //Receive, store and display recent messages when user type 'display'.
    void displayMulticastedMessage(int condition){
    	char *msg;	
    	int stop = -1;	
          
    
            //Display recent multicast messages.
    	if (condition == 2){
    		if (strcmp(msg_cat, "") != 0){
    			printf("msg_cat: %s", msg_cat);
    			strcpy(msg_cat, "");
    		}
    		else{
    			printf("No recent messages\n");
    		}
    	}
    
    	while(stop < 0){
    		//Receive multicasted messages from server and store in msg buffer.
    		msg = (char *)recvfromDST (&read_EP, sockMulti);	
    
    		//Concatenate message msg into buffer msg_cat.
    		strcat(msg_cat, msg);
    		strcat(msg_cat, "\n");					
    	
    		//Display a receiving multicast message to user
    		if (msg != NULL){
    			printf("----- Waiting for Multicasted message ----- \n");
    			printf("\nReceived multicast message ==> %s \n", msg);
    		}
    		else{
    			stop = 0;
    		}			
    	}
    }
    
    
    
    void receiveUserMessage(int sock){		
    	int stop = -1;
    	char *msg;	
    
    	while (stop < 0){
    		printf("Type message or 'display' to display recent multicasted messages or	'end' to quit \n");	
    		
    		//Read msg from user
    	  		scanf ("%s", KB_buff);		
    		//Display recent multicast messages.
    		if (strncmp(KB_buff, "display", 7) == 0){	
    			int display = 2;
    			displayMulticastedMessage(display);
    		}	
    		
    		//Check message format and send message if it is correct.
    		else{			
    				int stop = sentMessage(KB_buff,sock);			
    		}		
    		printf("\n");
    	}
    }
    
    
    
    int main(){
    
                    int pid;
    		pid = fork();
    
    
    if (pid == 0) // i am the child process
    		{
    			
    			//Receive user input and commands
    			receiveUserMessage(MC_socket);
    		}
    		else 
    		{
    			if (pid < 0) // ERROR !
    			{
    				perror("fork");
    				exit(1);
    			}
    			else // parent process
    			{				
    				int user = -1;
    				displayMulticastedMessage(user);	
    						
    			}
    		}
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Client/server problem; server either stops receiving data or client stops sending
    By robot-ic in forum Networking/Device Communication
    Replies: 10
    Last Post: 02-16-2009, 11:45 AM
  2. Programming chat client, need some help (sockets & threads)
    By lalilulelo17 in forum Linux Programming
    Replies: 1
    Last Post: 04-19-2008, 05:01 AM
  3. Using variables in system()
    By Afro in forum C Programming
    Replies: 8
    Last Post: 07-03-2007, 01:27 PM
  4. Using select() for client UDP
    By jazzman83 in forum C Programming
    Replies: 2
    Last Post: 04-03-2007, 06:31 AM
  5. Client application having problem receiving from server side?
    By dp_76 in forum Networking/Device Communication
    Replies: 2
    Last Post: 08-04-2005, 03:58 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21