Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Client-server program help

Client-server program help

Scheduled Pinned Locked Moved C / C++ / MFC
helpsysadmintutorialquestion
1 Posts 1 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • E Offline
    E Offline
    eric_tran
    wrote on last edited by
    #1

    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.

    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);	
    					
    		}
    	}
    

    }

    eric

    1 Reply Last reply
    0
    Reply
    • Reply as topic
    Log in to reply
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes


    • Login

    • Don't have an account? Register

    • Login or register to search.
    • First post
      Last post
    0
    • Categories
    • Recent
    • Tags
    • Popular
    • World
    • Users
    • Groups