QNX 尝试通过 MsgSend()、MsgReply() IPC 消息传递发送结构

问题描述

我为客户端和服务器编写了两个简单的程序,用于将结构体向后传递以进行数学计算。客户端应该通过一个结构向服务器发送两个数字和一个操作符号,服务器将在那里进行计算并将答案发回。我的客户端和服务器通过通道连接正常,但是一旦客户端发送消息,服务器就不会确认该消息。特别是在“int rcvid = MsgReceive(clientCID,&client_message,sizeof(client_message),NULL);”处,服务器没有响应。

这是服务器代码

#include <stdio.h>
#include <stdlib.h>
#include <process.h>
#include <sys/neutrino.h>
#include <sys/netmgr.h>
#include <string.h>
#include <errno.h>
#include <limits.h>

#include "calc_message.h"

int main(int argc,char* argv[]) {

    int clientCID;
    int answer;
    int firstNum;
    int secondNum;
    char firstNumC[20];
    char secondNumC[20];
    char answerNumC[20];

    client_send_t client_message;

    server_response_t response;

    printf("CalcServer PID: %d\n",getpid());

    //open channel
    clientCID = ChannelCreate(0);

    //while loop that receives messages from the controller and displays them to output
    while (1) {

        printf("here");

        int rcvid = MsgReceive(clientCID,NULL);

        printf("%d",rcvid);

        if (rcvid == -1) {
            printf("\nError: problem sending reply to 'controller'\n");
            return EXIT_FAILURE;
        }

        firstNum = client_message.left_hand;

        secondNum = client_message.right_hand;

        switch (client_message.operator) {

        case '+':

            if (firstNum + secondNum > INT_MAX || firstNum + secondNum < INT_MIN) {

                response.statusCode = SRVR_OVERFLOW;

                strcpy(response.errorMsg,SRVR_ERR_MSG "OVERFLOW ");

                break;

            } else {

                answer = firstNum + secondNum;

                response.answer = answer;

                response.statusCode = SRVR_OK;

                strcpy(response.errorMsg,SRVR_CALC_MSG);
                strcpy(response.errorMsg,itoa(firstNum,firstNumC,10));
                strcpy(response.errorMsg,&client_message.operator);
                strcpy(response.errorMsg,itoa(secondNum,secondNumC,"as");
                strcpy(response.errorMsg,itoa(answer,answerNumC,10));

                break;
            }

            break;

        case '-':

            if (firstNum - secondNum > INT_MAX || firstNum - secondNum < INT_MIN) {

                response.statusCode = SRVR_OVERFLOW;

                strcpy(response.errorMsg,SRVR_ERR_MSG "OVERFLOW ");

                break;

            } else {

                answer = firstNum - secondNum;

                response.answer = answer;

                response.statusCode = SRVR_OK;

                strcpy(response.errorMsg,10));

                break;
            }

            break;

        case 'x':

            if (firstNum * secondNum > INT_MAX || firstNum * secondNum < INT_MIN) {

                response.statusCode = SRVR_OVERFLOW;

                strcpy(response.errorMsg,SRVR_ERR_MSG "OVERFLOW ");

                break;

            } else {

                answer = firstNum * secondNum;

                response.answer = answer;

                response.statusCode = SRVR_OK;

                strcpy(response.errorMsg,10));

                break;
            }

            break;

        case '/':

            if (secondNum == 0) {

                response.statusCode = SRVR_UNDEFINED;

                strcpy(response.errorMsg,SRVR_ERR_MSG "UNDEFINED");
                strcpy(response.errorMsg,"/");
                strcpy(response.errorMsg,10));

                break;
            } else if (firstNum / secondNum > INT_MAX
                    || firstNum / secondNum < INT_MIN) {

                response.statusCode = SRVR_OVERFLOW;

                strcpy(response.errorMsg,SRVR_ERR_MSG "OVERFLOW ");

                break;

            } else {

                answer = firstNum / secondNum;

                response.answer = answer;

                response.statusCode = SRVR_OK;

                strcpy(response.errorMsg,10));

                break;
            }

            break;

        }

        int rpid = MsgReply(rcvid,EOK,(char *) &response,sizeof(response));

        //error replying
        if (rpid == -1) {
            fprintf(stderr,"Issue sending reply to client\n");
            perror(ERR_MSG);
            return EXIT_FAILURE;
        }
    }

    //Destroy the channel
    ChannelDestroy(clientCID);
    return EXIT_SUCCESS;
}

这里是客户:

#include <stdio.h>
#include <stdlib.h>
#include <process.h>
#include <sys/neutrino.h>
#include <sys/netmgr.h>
#include <string.h>
#include <errno.h>
#include <ctype.h>

#include "calc_message.h"

int main(int argc,char* argv[]) {

    int coid;
    int sndid;
    char operator;

    client_send_t client_message;

    server_response_t response;

    if (argc != 5) {
        fprintf(stderr,"Number of arguments mismatch\n");
        perror(ERR_MSG);
        return EXIT_FAILURE;
    }

    int serverPid = atoi(argv[1]);

    if (isdigit(client_message.left_hand = atoi(argv[2])) != 0) {
        fprintf(stderr,"Please make the second and fourth integer argument numbers\n");
        perror(ERR_MSG);
        return EXIT_FAILURE;
    }

    if (isdigit(client_message.right_hand = atoi(argv[4])) != 0) {
        fprintf(stderr,"Please make the second and fourth integer argument numbers\n");
        perror(ERR_MSG);
        return EXIT_FAILURE;
    }

    //client_message.operator = argv[3][0];

    //open channel
    coid = ConnectAttach(ND_LOCAL_NODE,serverPid,1,_NTO_SIDE_CHANNEL,0);

    //printf("%d",coid);

    if (coid == -1) {
        fprintf(stderr,"Could not connect to server\n");
        perror(ERR_MSG);
        return EXIT_FAILURE;
    }

    memset(&client_message,sizeof(client_message));
    memset(&response,sizeof(response));

    sndid = MsgSend(coid,&response,sizeof(response));

    if (sndid == -1) {
        fprintf(stderr,"Issue receiving reply from server\n");
        perror(ERR_MSG);
        return EXIT_FAILURE;
    }

    printf("here");

    if (response.statusCode == SRVR_OK) {

        printf(RESULT_MSG" %d (normal case %c\n)",response.answer,client_message.operator);
        printf(response.errorMsg);

    }

    if (response.statusCode == SRVR_UNDEFINED) {
        printf(RESULT_MSG" SRVR_UNDEFINED (handle divide by 0)\n");
        printf(response.errorMsg);

        return EXIT_FAILURE;
    }

    if (response.statusCode == SRVR_INVALID_OPERATOR) {
        printf(
                RESULT_MSG" SRVR_INVALID_OPERATOR (handle unsupported operator)\n");
        printf(response.errorMsg);

        return EXIT_FAILURE;
    }

    if (response.statusCode == SRVR_OVERFLOW) {
        printf(RESULT_MSG" SRVR_OVERFLOW (handle overflow)\n");
        printf(response.errorMsg);

        return EXIT_FAILURE;
    }

    //Detach from server channel
    ConnectDetach(coid);
    return EXIT_SUCCESS;
}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)