Question: The following codes are to send a message queue from the client to the server. Then, the server will send the message back as an

The following codes are to send a message queue from the client to the server. Then, the server will send the message back as an upper case to the client.

My question is how can I make the client send another message to confirm that "the client got the message"?

local.h:

#define _GNU_SOURCE #include #include #include #include #include #include const char SEED ='M'; // Common seed for ftok const long int SERVER=1L; // Message type for server typedef struct { long int msg_to; // Message in queue for this type long int msg_fm; // Placed in the queue by this type char buffer[BUFSIZ]; // The actual message }MESSAGE; using namespace std;

client code:

#include "local.h" #include using namespace std; int main( ){ key_t key; pid_t cli_pid; int mid, n; MESSAGE msg; static char m_key[10]; cli_pid = getpid( ); if ((key = ftok(".", SEED)) == -1) { perror("Client: key generation"); return 1; } if ((mid=msgget(key, 0 )) == -1 ) { mid = msgget(key,IPC_CREAT | 0660); switch (fork()) { case -1: perror("Client: fork"); return 2; case 0: sprintf(m_key, "%d", mid); execlp("./server", "server", m_key, "&", 0); perror("Client: exec"); return 3; } } while (1) { msg.msg_to = SERVER; msg.msg_fm = cli_pid; write(fileno(stdout), "cmd> ", 6); memset(msg.buffer, 0x0, BUFSIZ); if ( (n=read(fileno(stdin), msg.buffer, BUFSIZ)) == 0 ) break; n += sizeof(msg.msg_fm); if (msgsnd(mid, &msg, n, 0) == -1 ) { perror("Client: msgsend"); return 4; } if( (n=msgrcv(mid, &msg, BUFSIZ, cli_pid, 0)) != -1 ) write(fileno(stdout), msg.buffer, n); } msgsnd(mid, &msg, 0, 0); return 0; }

the server code:

#include "local.h" #include #include #include #include using namespace std; int main(int argc, char *argv[ ]) { int mid, n; MESSAGE msg; void process_msg(char *, int); if (argc != 3) { cerr << "Usage: " << argv[0] << " msq_id &" << endl; return 1; } mid = atoi(argv[1]); while (1) { memset( msg.buffer, 0x0, BUFSIZ ); if ((n=msgrcv(mid, &msg, BUFSIZ, SERVER, 0)) == -1 ) { perror("Server: msgrcv"); return 2; } else if (n == 0) break; process_msg(msg.buffer, strlen(msg.buffer)); msg.msg_to = msg.msg_fm; msg.msg_fm = SERVER; n += sizeof(msg.msg_fm); if (msgsnd(mid, &msg, n, 0) == -1 ) { perror("Server: msgsnd"); return 3; } } msgctl(mid, IPC_RMID, (struct msqid_ds *) 0 ); exit(0); } /* Convert lower case alphabetics to upper case */ void process_msg(char *b, int len){ for (int i = 0; i < len; ++i) if (isalpha(*(b + i))) *(b + i) = toupper(*(b + i)); }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!