RPC 浮动结果错误

RPC float result error

我正在编写一个简单的 RPC 程序,用于对两个 int 数字进行加减乘除运算。我写了 int 然后我用 int 生成文件。当我执行服务器时,客户端给了它两个简单的整数,服务器给了大浮点数。

怎么了?

file.x如下:

struct entier{
int a;
int b;};
typedef struct entier params;
struct resultat{
float result;
int   resp;};
typedef struct resultat res;
program CALC{
version VERS1{
    void CALCUL_NULL(void)=0;
    res ADDITION(params )=1;
    res SOUTRACTION(params)=2;
    res MULTIPLICATION(params)=3;
    res DIVISION(params)=4;
         }=1;
}=0x20000001;

服务器代码是:

/*
 * This is sample code generated by rpcgen.
 * These are only templates and you can use them
 * as a guideline for developing your own functions.
 */

#include "file.h"

void *
calcul_null_1_svc(void *argp, struct svc_req *rqstp)
{
    static char * result;

    /*
     * insert server code here
     */

    return (void *) &result;
}

res *
addition_1_svc(params *argp, struct svc_req *rqstp)
{
    static res  result;

    result.result= argp->a + argp->b;
    result.resp=0;

    return &result;
}

res *
soutraction_1_svc(params *argp, struct svc_req *rqstp)
{
    static res  result;

    result.result= argp->a - argp->b;
    result.resp=0;

    return &result;
}

res *
multiplication_1_svc(params *argp, struct svc_req *rqstp)
{
    static res  result;

    result.result= argp->a * argp->b;
    result.resp=0;

    return &result;
}

res *
division_1_svc(params *argp, struct svc_req *rqstp)
{
    static res  result;

    if(argp->b ==0)
    result.resp=1;
    else
        result.result= argp->a / argp->b;

    return &result;
}

客户:

/*
* This is sample code generated by rpcgen.
 * These are only templates and you can use them
 * as a guideline for developing your own functions.
 */

#include "file.h"

int x;
int y;
void
calc_1(char *host)
{
    CLIENT *clnt;
    void  *result_1;
    char *calcul_null_1_arg;

    params  arguments;
    arguments.a=(float)x;
    arguments.b=(float)y;
    res  *respons;


#ifndef DEBUG
    clnt = clnt_create (host, CALC, VERS1, "udp");
    if (clnt == NULL) {
        clnt_pcreateerror (host);
        exit (1);
    }
    #endif  /* DEBUG */

    result_1 = calcul_null_1((void*)&calcul_null_1_arg, clnt);
    if (result_1 == (void *) NULL) {
        clnt_perror (clnt, "call failed");
    }
    respons = addition_1(&arguments, clnt);
    printf("the additon is%f \n",respons->result);
    if (respons == (res *) NULL) {
        clnt_perror (clnt, "call failed");
    }
    respons = soutraction_1(&arguments, clnt);
    printf("the substraction is %f \n",respons->result);
    if (respons == (res *) NULL) {
        clnt_perror (clnt, "call failed");
    }
    respons = multiplication_1(&arguments, clnt);
        printf("the multiplication is %f \n",respons->result);
    if (respons == (res *) NULL) {
    clnt_perror (clnt, "call failed");
    }
    respons = division_1(&arguments, clnt);
    if (respons == (res *) NULL) {
        clnt_perror (clnt, "call failed");
    if(respons->resp==1)
    printf("error division by zero");
    else printf("the division is %f \n",respons->result);
    }
#ifndef DEBUG
    clnt_destroy (clnt);
#endif   /* DEBUG */
}

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

    if (argc < 4) {
        printf ("usage: %s server_host\n", argv[0]);
        exit (1);
    }
    host = argv[1];
    x=(int) argv[2];
    y= (int) argv[3];
    calc_1 (host);
exit (0);
}

执行:

blitzkrieg@blitzkrieg-TravelMate-P253:~/Desktop/rpc$ ./client localhost 5 6
the additon is2131577472.000000
the substraction is -2.000000
the multiplication is -88320960.000000
blitzkrieg@blitzkrieg-TravelMate-P253:~/Desktop/rpc$

您似乎将 entier.aentier.b 定义为整数,但将它们作为浮点数传递。我希望这会在编译时产生错误,但如果没有,那么它可能会尝试对浮点数进行整数运算,这不会很好地工作。

struct entier{
  int a;
  int b;
};

typedef struct entier params;

params  arguments;
arguments.a=(float)x;
arguments.b=(float)y;

问题2

x=(int) argv[2];
y= (int) argv[3];

在 C 中,这将采用 char * 并尝试将其作为 int 处理,这也不会执行您想要的操作。您需要使用某种转换函数来将它们作为整数获取。 atoi() 在这里可以满足您的需求。

由于从 intfloatchar[] 的大量转换问题,您确定在编译时没有收到关于此的警告吗,因为此代码不应按原样运行至 int.