RPC浮点结果错误

时间:2015-06-01 20:43:15

标签: c rpc

我正在编写一个简单的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$

1 个答案:

答案 0 :(得分:0)

您似乎将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的大量转换问题,您确定在编译时没有收到有关此问题的警告,因为此代码不应按原样运行{1}}。