首页 > 解决方案 > 消息队列 - 服务器只响应一种类型的消息

问题描述

我正在编写一个服务器客户端应用程序,它允许用户向服务器发送通信并且服务器响应用户。命令是 TIME(服务器响应包含当前时间的字符串)和 END(现在它只是发送一些文本字符串)。问题是,TIME 命令工作得很好,服务器收到消息并做出响应。当我在控制台中输入任何不是 TIME 的单词时,什么都没有发生,程序只是再次要求用户输入。使用 END 命令时也会发生同样的事情。这些程序的示例输出: 客户: 在此处输入图像描述

服务器:

在此处输入图像描述

客户端代码如下所示:

#include <stdio.h>
#include <stdlib.h>
#include <sys/msg.h>
#include <signal.h>
#include <string.h>
#include "qdetails.h"
#include <unistd.h>
#include <sys/types.h>
#include <pwd.h>
#include <stddef.h>


int main()
{

struct msgbuf message;
struct msgbuf response;
char* homedir = getenv("HOME");
   key_t serverkey, ckey;
   int msgid, clientQ;

   serverkey= ftok(homedir, 65);
   ckey = ftok(homedir, 128);
   clientQ = msgget(IPC_PRIVATE, 0660);
   msgid = msgget(serverkey, 0666 | IPC_CREAT);
   message.mtype = 1;
   printf("Server message queue id: %d \n Client queue id : %d\n", msgid, clientQ);
   sprintf(message.mtext, "%d",  clientQ);
   msgsnd(msgid, &message, sizeof(message), 0);
   printf("Sent client queue id  : %s  to server queue : %d\n", message.mtext, msgid);

   while(1){
    char command[5];
    printf("Commands available: TIME for current time, END to end the program\n");
    fgets(command, sizeof(command), stdin);
    if(strcmp(command, "TIME")==0){
        message.mtype = 2;
        msgsnd(msgid, &message, sizeof(message), 0);
        printf("TIME request sent\n");
        msgrcv(clientQ, &response, sizeof(response), 0,0);

        printf("Current time is: %s\n", response.mtext);
        printf("\n\n");
        command == "";
        }
        else if (strcmp(command, "END") == 0 ){
        message.mtype = 3;
        msgsnd(msgid, &message, sizeof(message), 0);
        } else {
        continue;
        }

   }
}

服务器看起来像这样:

#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/msg.h>
#include <signal.h>
#include "qdetails.h"
#include <string.h>
int main()
{

   char* homedir = getenv("HOME");
   struct msgbuf message;
   struct msgbuf send;
   send.mtype = 4;
   key_t serverkey;
   int msgqid;


   serverkey = ftok(homedir, 65);
   msgqid = msgget(serverkey, 0666 | IPC_CREAT);
   printf("Server queue is active, queue id: %d ", msgqid);
    if((msgrcv(msgqid, &message, sizeof(message), 0, 0) == -1)){
      perror("Failed to receive message");
      }

    printf("Received Message is : %s \n", message.mtext);
   while(1){




   if((msgrcv(msgqid, &message, sizeof(message), 0, 0) == -1)){
      perror("Failed to receive message");
      }


    if(message.mtype == 2){
    printf("TIME request received\n");
    time_t t = time(NULL);
    struct tm *tm = localtime(&t);
    char s[64];
    assert(strftime(s, sizeof (s), "%c", tm));
    sprintf(send.mtext, "%s", s);
    printf("%s\n", s);
    int to = atoi(message.mtext);
    printf("%d",to);
        if((msgsnd(to, &send, sizeof(send),0)==-1)){
        perror("Error while responding");
        }

   } else if(message.mtype == 3){
    printf("END request received\n");
     int to = atoi(message.mtext);
     strcpy("Respond from server: ", send.mtext);
     if(msgsnd(to, &send, sizeof(send), 0) == -1){
        printf("Error while responding to END request\n");
     }
    }
   }
   msgctl(msgqid, IPC_RMID, NULL); //destroy the message queue
   return 0;
}

我不确定这里缺少什么,感谢各种建议

标签: cipcmessage-queue

解决方案


推荐阅读