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



LinkBack URL
About LinkBacks


