2015-06-01 38 views
0

我正在寫一個簡單的RPC程序,它可以對兩個int數字進行加,減,乘和除。我寫了int,然後我用int來生成文件。當我執行服務器時,客戶端給它兩個簡單的整數,服務器給出大的浮點數。RPC浮點數結果錯誤

出了什麼問題?

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); 
} 

執行:

[email protected]:~/Desktop/rpc$ ./client localhost 5 6 
the additon is2131577472.000000 
the substraction is -2.000000 
the multiplication is -88320960.000000 
[email protected]:~/Desktop/rpc$ 

回答

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

+0

是的這是一個更新,但在我通過它們之前整數但仍然顯示相同 –

+0

沒有編譯錯誤shur –

+0

我希望至少有一些警告。無論如何,將您的輸入字符串轉換爲整數,不要將它們轉換爲浮點數,然後重試。我還會通過一個'printf(「%d%d \ n」,arguments.a,arguments.a)'輸出來確保它們是你期望的數字。 –