简体   繁体   中英

design a Unix message queue server for multiple clients

What should I modify in the below codes in order to use only one message queue for one server and multiple clients. I'm pretty sure I need to assign different values to msgid and then use that to fetch the messages from the message queue but not completely sure if I'm right and how to implement it. I would be grateful for any help.

Code1:

struct my_msg_st { 
     long int my_msg_type; 
     char some_text[BUFSIZ];
};

int main() {
    int running = 1;
    int msgid;
    struct my_msg_st some_data; 
    long int msg_to_receive = 0

    msgid = msgget((key_t)1234, 0666 | IPC_CREAT);

    if (msgid == -1) {
         fprintf(stderr, “msgget failed with error: %d\n”, errno);
         exit(EXIT_FAILURE);
    }

    while(running) {
        if (msgrcv(msgid, (void *)&some_data, BUFSIZ, msg_to_receive, 0) == -1) {
              fprintf(stderr, “msgrcv failed with error: %d\n”, errno); 
              exit(EXIT_FAILURE);
        }
        printf(“You wrote: %s”, some_data.some_text);
        if (strncmp(some_data.some_text, “end”, 3) == 0) {
              running = 0;
        }
    }

    if (msgctl(msgid, IPC_RMID, 0) == -1) { 
        fprintf(stderr, “msgctl(IPC_RMID) failed\n”);         
        exit(EXIT_FAILURE);
    }

    exit(EXIT_SUCCESS); 
}

Code 2:

#define MAX_TEXT 512

struct my_msg_st {
     long int my_msg_type; char some_text[MAX_TEXT];
};

int main() {
     int running = 1;
     struct my_msg_st some_data; int msgid;
     char buffer[BUFSIZ];
     msgid = msgget((key_t)1234, 0666 | IPC_CREAT);

     if (msgid == -1) {
           fprintf(stderr, “msgget failed with error: %d\n”, errno);         
           exit(EXIT_FAILURE);
     }

     while(running) {
           printf(“Enter some text: “); 
           fgets(buffer, BUFSIZ, stdin); 
           some_data.my_msg_type = 1; 
           strcpy(some_data.some_text, buffer); 

           if (msgsnd(msgid, (void *)&some_data, MAX_TEXT, 0) == -1) {          
                 fprintf(stderr, “msgsnd failed\n”);
                 exit(EXIT_FAILURE);
           }

           if (strncmp(buffer, “end”, 3) == 0) {
                 running = 0;
           }

     }
     exit(EXIT_SUCCESS); 
 }

I suggest that you create a single queue and each message that you push to the queue should have a different value for my_msg_type instead of hard coding it to 1 as you have done. This is the mapping between the client and server. Each client can be numbered from 1 till n.

some_data.my_msg_type = client_id;

Once this is done in each client you can call msgrcv with its corresponding client ID. This can be done by using the client ID as the 4th argument in msgrcv.

msgrcv(msgid, (void *)&some_data, BUFSIZ, msg_to_receive, client_id)

This way you have a single server generating data for multiple clients.

Hope that helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM