简体   繁体   English

为多个客户端设计一个 Unix 消息队列服务器

[英]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.我很确定我需要为msgid分配不同的值,然后使用它从消息队列中获取消息,但不能完全确定我是否正确以及如何实现它。 I would be grateful for any help.我将不胜感激任何帮助。

Code1:代码1:

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:代码 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.我建议您创建一个队列,并且您推送到队列的每条消息都应该具有不同的 my_msg_type 值,而不是像您所做的那样将其硬编码为 1。 This is the mapping between the client and server.这是客户端和服务器之间的映射。 Each client can be numbered from 1 till n.每个客户端可以从 1 到 n 编号。

some_data.my_msg_type = client_id;

Once this is done in each client you can call msgrcv with its corresponding client ID.在每个客户端中完成此操作后,您可以使用其相应的客户端 ID 调用 msgrcv。 This can be done by using the client ID as the 4th argument in msgrcv.这可以通过使用客户端 ID 作为 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!希望有帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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