RPC (원격 프로 시저 호출)를 사용하여 서버 클라이언트 프로그램을 만들고 있습니다. 클라이언트는 서버에 세 개의 숫자를 보내고 서버는 숫자의 합을 만들고 서버가 현재 세 개의 숫자를 클라이언트가 보낸 것보다 현재 합계보다 크게 만듭니다. 하지만 서버가 클라이언트의 포트와 IP를 다시 보내주기를 원하며 어떻게해야할지 모르겠다. 이 문제를 해결하는 쉬운 방법이 있습니까? 여기 원격 프로 시저 호출 용 포트 및 ip 유닉스
내 코드입니다 :headerrpc.h
#include <rpc/rpc.h>
#include <rpc/xdr.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#define PROGRAM_EXEC ((u_long)0x40000000)
#define VERSIUNE_EXEC ((u_long)1)
#define EXEC_ADD ((u_long)2)
typedef struct Data{
short a;
short b;
short c;
u_short port;
}Data;
int xdr_Data(XDR* xdr, Data* d){
if(xdr_short(xdr,&(d->a)) == 0) return 0;
if(xdr_short(xdr,&(d->b)) == 0) return 0;
if(xdr_short(xdr,&(d->c)) == 0) return 0;
if(xdr_short(xdr,&(d->port)) == 0) return 0;
return 1;
}
서버
#include "headerrpc.h"
#include <stdio.h>
#include <stdlib.h>
Data* data;
Data* add(Data* d){
static int sum = -32000;
//Data* data = (Data*)malloc(sizeof(Data));
if((d->a + d->b + d->c) > sum)
{
sum = d->a + d->b + d->c;
data->a = d->a;
data->b = d->b;
data->c = d->c;
data->port = data->port;
printf("It was found a greater sum %d\n");
return data;
}
else
{
printf("The sum of the given numbers is not greater than the current sum\n");
return data;
}
}
main(){
data = (Data*)malloc(sizeof(Data));
registerrpc(PROGRAM_EXEC, VERSIUNE_EXEC, EXEC_ADD, add, xdr_Data, xdr_Data);
svc_run();
}
클라이언트
#include "headerrpc.h"
#include <stdio.h>
#include <stdlib.h>
int main()
{
Data d;
Data* r = (Data*)malloc(sizeof(Data));
int suma;
printf("Client\n");
int a, b, c;
printf("Type the first number: ");
scanf("%d",&a);
printf("Type the second number: ");
scanf("%d",&b);
printf("Type the third number: ");
scanf("%d",&c);
d.a = a;
d.b = b;
d.c = c;
d.port = serv_addr.sin_port;
callrpc("localhost",PROGRAM_EXEC, VERSIUNE_EXEC,EXEC_ADD,(xdrproc_t)xdr_Data,(char*)&d,(xdrproc_t)xdr_Data,(char*)r);
printf("The numbers with the greater sum are: %d, %d, %d\n", r->a,r->b,r->c);
}