Client-server program help
-
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