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$
是的這是一個更新,但在我通過它們之前整數但仍然顯示相同 –
沒有編譯錯誤shur –
我希望至少有一些警告。無論如何,將您的輸入字符串轉換爲整數,不要將它們轉換爲浮點數,然後重試。我還會通過一個'printf(「%d%d \ n」,arguments.a,arguments.a)'輸出來確保它們是你期望的數字。 –