[英]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.a
和entier.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()
可能會在這里滿足您的需求。
您確定在編譯時沒有收到關於此的警告,因為由於從int
到float
以及從char[]
到int
大量轉換問題,該代碼不應按原樣運行。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.