2014-01-09 2 views
0

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

답변

0

기본 서버 루틴은 실제로 두 번째 매개 변수를 struct svc_req *rqstp에서 클라이언트의 IP 주소를 확인할 수 있습니다. 당신은 rqstp->rq_xprt->xp_raddr.sin_port 멤버에서 rqstp->rq_xprt->xp_raddr.sin_addr 회원, 저두에서 포트를 클라이언트의 IP 주소를 확인할 수 있습니다

Data* add(Data* d, struct svc_req *rqstp) { 

: 같은

그래서 당신의 추가 기능 정의가 보일 것입니다.

registerrpc을 사용하고 있기 때문에 서비스가 UDP 이상에서만 사용 가능하며 IPv6을 통해 사용할 수 없다는 것을 의미합니다. 즉, 주소는 32 비트 값으로 반환 할 수 있습니다.

RPC over IPv6에 대한 경험이 부족하여 해당 상황에서 작동하는 대답을 줄 수 없습니다.