gpt4 book ai didi

c - 使用 rpcgen 的简单计算器

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:01:54 25 4
gpt4 key购买 nike

我是 C 语言编程的新手。我尝试创建一个小而简单的程序来将文件 calculs.x 中的两个整数相加

这里是calculs.x文件的内容

/* calculs.x*/

struct data_in {
int arg1;
int arg2;
};
typedef struct data_in data_in;

struct result_int {
int result;
int errno;
};

struct result_float {
int result;
int errno;
};

typedef struct result_int result_int;
typedef struct result_float result_float;

program CALCULS{
version VERSION_UN{
void CALCULS_NULL(void) = 0;
result_int ADD (data_in) = 1;
result_int SUB(data_in) = 2;
result_int MUL(data_in) = 3;
result_float DIV (data_in) = 4;
} = 1;
} = 0x20000001;

我第一次为客户创建了一个文件calculs.c:

#include <rpc/rpc.h>
#include "calculs.h"

int main(int argc, char *argv[]) {
int buffer[256];
struct data_in input;
struct result_int *output;
CLIENT *cl;

if (argc != 2) {
printf("usage: client hostname_of_server\n");
exit(1);
}

/*Etablir le lien vers le serveur distant
* cl = clnt_create(server, PROG, VERS, prot);
*/
cl = clnt_create(argv[1], CALCULS, VERSION_UN, "tcp");
if (cl == NULL) {
clnt_pcreateerror(argv[1]);
exit(1);
}

input.arg1 = 5;
input.arg2 = 5;

output = add_1(&input, cl);
if (output == NULL) {
clnt_perror(cl, argv[1]);
exit(1);
}
printf("the result field is %d\n", output->result);
printf("the errno field is %d\n", output->errno);

clnt_destroy(cl);

return 0;
}

我在编译这个文件时没有收到任何错误,但是对于另一个 rcalculs.c 文件,我无法编译。这是文件内容 rcalculs.c:

#include <rpc/rpc.h>
#include "calculs.h"

result_int *add_1(struct data_in data, struct svc_req *rqstp) {
int buffer;
struct result_int result;
int a = data.arg1;
int b = data.arg2;
buffer = a+b;
result.result = buffer;
result.errno =0;
return result;
}

编译报错是

rcalculs.c:11:13: erreur: conflicting types for ‘add_1’
In file included from rcalculs.c:9:0:
calculs.h:46:22: note: previous declaration of ‘add_1’ was here
rcalculs.c: In function ‘add_1’:
rcalculs.c:19:5: erreur: incompatible types when returning type ‘struct result_int’ but ‘struct result_int *’ was expected

你能帮我解决这个问题吗?

最佳答案

按如下方式使用您的 calculs.x。

/* calculs.x*/

struct data_in {
int arg1;
int arg2;
};
typedef struct data_in data_in;

struct result_int {
int result;
int errno;
};

struct result_float {
int result;
int errno;
};

typedef struct result_int result_int;
typedef struct result_float result_float;

program CALCULS{
version VERSION_UN{
void CALCULS_NULL(void) = 0;
result_int ADD (data_in) = 1;
result_int SUB(data_in) = 2;
result_int MUL(data_in) = 3;
result_float DIV (data_in) = 4;
} = 1;
} = 0x20000001;

对于您的客户 calculs.c 用户

#include <rpc/rpc.h>
#include "calculs.h"

result_int *add_1(struct data_in data, struct svc_req *rqstp);

int main(int argc, char *argv[]) {
int buffer[256];
struct data_in input;
struct result_int *output;
CLIENT *cl;

if (argc != 2) {
printf("usage: client hostname_of_server\n");
exit(1);
}

/*Etablir le lien vers le serveur distant
* cl = clnt_create(server, PROG, VERS, prot);
*/
cl = clnt_create(argv[1], CALCULS, VERSION_UN, "tcp");
if (cl == NULL) {
clnt_pcreateerror(argv[1]);
exit(1);
}

input.arg1 = 5;
input.arg2 = 5;

output = add_1(&input, cl);
if (output == NULL) {
clnt_perror(cl, argv[1]);
exit(1);
}
printf("the result field is %d\n", output->result);
printf("the errno field is %d\n", output->errno);

clnt_destroy(cl);

return 0;
}

rcalculs.c 的内容:

#include <rpc/rpc.h>
#include "calculs.h"

result_int *add_1(struct data_in data, struct svc_req *rqstp) {
int buffer;
struct result_int result;
int a = data.arg1;
int b = data.arg2;
buffer = a+b;
result.result = buffer;
result.errno =0;
return &result;
}

让我知道它是否有效。

关于c - 使用 rpcgen 的简单计算器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19216957/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com