• RPC简单使用


    test.x

    1 program TESTPROG {
    2 version TESTVERS {
    3 int Test(string) = 1;
    4 } = 1;
    5 } = 0x30000000;


    test_local.c

     1 #include <stdio.h>
    2 #include <stdlib.h>
    3
    4 #include "test.h"
    5
    6 #define HOST "localhost"
    7
    8 int test(char *);
    9
    10 CLIENT * handle;
    11
    12 int main(int argc, char **argv)
    13 {
    14 handle = clnt_create(argv[1], TESTPROG, TESTVERS, "tcp");
    15 if (handle == NULL) {
    16 printf("connect errror\n");
    17 exit(1);
    18 }
    19 char a[100] = "gaga";
    20 printf("%d\n", test(a));
    21
    22 return 0;
    23 }

    test_remote.c

    1 #include <rpc/rpc.h>
    2 #include <string.h>
    3
    4 #include "test.h"
    5
    6 int test(char * str)
    7 {
    8 return strlen(str);
    9 }

    执行rpggen test.x生成三个文件

    test.h

     1 /*
    2 * Please do not edit this file.
    3 * It was generated using rpcgen.
    4 */
    5
    6 #ifndef _TEST_H_RPCGEN
    7 #define _TEST_H_RPCGEN
    8
    9 #include <rpc/rpc.h>
    10
    11
    12 #ifdef __cplusplus
    13 extern "C" {
    14 #endif
    15
    16
    17 #define TESTPROG 0x30000000
    18 #define TESTVERS 1
    19
    20 #if defined(__STDC__) || defined(__cplusplus)
    21 #define Test 1
    22 extern int * test_1(char **, CLIENT *);
    23 extern int * test_1_svc(char **, struct svc_req *);
    24 extern int testprog_1_freeresult (SVCXPRT *, xdrproc_t, caddr_t);
    25
    26 #else /* K&R C */
    27 #define Test 1
    28 extern int * test_1();
    29 extern int * test_1_svc();
    30 extern int testprog_1_freeresult ();
    31 #endif /* K&R C */
    32
    33 #ifdef __cplusplus
    34 }
    35 #endif
    36
    37 #endif /* !_TEST_H_RPCGEN */

    test_clnt.c

     1 /*
    2 * Please do not edit this file.
    3 * It was generated using rpcgen.
    4 */
    5
    6 #include <memory.h> /* for memset */
    7 #include "test.h"
    8
    9 /* Default timeout can be changed using clnt_control() */
    10 static struct timeval TIMEOUT = { 25, 0 };
    11
    12 int *
    13 test_1(char **argp, CLIENT *clnt)
    14 {
    15 static int clnt_res;
    16
    17 memset((char *)&clnt_res, 0, sizeof(clnt_res));
    18 if (clnt_call (clnt, Test,
    19 (xdrproc_t) xdr_wrapstring, (caddr_t) argp,
    20 (xdrproc_t) xdr_int, (caddr_t) &clnt_res,
    21 TIMEOUT) != RPC_SUCCESS) {
    22 return (NULL);
    23 }
    24 return (&clnt_res);
    25 }
    26
    27 extern CLIENT * handle;
    28 static int * ret;
    29 int test(char * str)
    30 {
    31 char ** arg = &str;
    32 ret = test_1(arg, handle);
    33 return (ret==0)?0:*ret;
    34 }

    test_svc.c

     1 /*
    2 * Please do not edit this file.
    3 * It was generated using rpcgen.
    4 */
    5
    6 #include "test.h"
    7 #include <stdio.h>
    8 #include <stdlib.h>
    9 #include <rpc/pmap_clnt.h>
    10 #include <string.h>
    11 #include <memory.h>
    12 #include <sys/socket.h>
    13 #include <netinet/in.h>
    14
    15 #ifndef SIG_PF
    16 #define SIG_PF void(*)(int)
    17 #endif
    18
    19 static void
    20 testprog_1(struct svc_req *rqstp, register SVCXPRT *transp)
    21 {
    22 union {
    23 char *test_1_arg;
    24 } argument;
    25 char *result;
    26 xdrproc_t _xdr_argument, _xdr_result;
    27 char *(*local)(char *, struct svc_req *);
    28
    29 switch (rqstp->rq_proc) {
    30 case NULLPROC:
    31 (void) svc_sendreply (transp, (xdrproc_t) xdr_void, (char *)NULL);
    32 return;
    33
    34 case Test:
    35 _xdr_argument = (xdrproc_t) xdr_wrapstring;
    36 _xdr_result = (xdrproc_t) xdr_int;
    37 local = (char *(*)(char *, struct svc_req *)) test_1_svc;
    38 break;
    39
    40 default:
    41 svcerr_noproc (transp);
    42 return;
    43 }
    44 memset ((char *)&argument, 0, sizeof (argument));
    45 if (!svc_getargs (transp, (xdrproc_t) _xdr_argument, (caddr_t) &argument)) {
    46 svcerr_decode (transp);
    47 return;
    48 }
    49 result = (*local)((char *)&argument, rqstp);
    50 if (result != NULL && !svc_sendreply(transp, (xdrproc_t) _xdr_result, result)) {
    51 svcerr_systemerr (transp);
    52 }
    53 if (!svc_freeargs (transp, (xdrproc_t) _xdr_argument, (caddr_t) &argument)) {
    54 fprintf (stderr, "%s", "unable to free arguments");
    55 exit (1);
    56 }
    57 return;
    58 }
    59
    60 int
    61 main (int argc, char **argv)
    62 {
    63 register SVCXPRT *transp;
    64
    65 pmap_unset (TESTPROG, TESTVERS);
    66
    67 transp = svcudp_create(RPC_ANYSOCK);
    68 if (transp == NULL) {
    69 fprintf (stderr, "%s", "cannot create udp service.");
    70 exit(1);
    71 }
    72 if (!svc_register(transp, TESTPROG, TESTVERS, testprog_1, IPPROTO_UDP)) {
    73 fprintf (stderr, "%s", "unable to register (TESTPROG, TESTVERS, udp).");
    74 exit(1);
    75 }
    76
    77 transp = svctcp_create(RPC_ANYSOCK, 0, 0);
    78 if (transp == NULL) {
    79 fprintf (stderr, "%s", "cannot create tcp service.");
    80 exit(1);
    81 }
    82 if (!svc_register(transp, TESTPROG, TESTVERS, testprog_1, IPPROTO_TCP)) {
    83 fprintf (stderr, "%s", "unable to register (TESTPROG, TESTVERS, tcp).");
    84 exit(1);
    85 }
    86
    87 svc_run ();
    88 fprintf (stderr, "%s", "svc_run returned");
    89 exit (1);
    90 /* NOTREACHED */
    91 }
    92 int test(char *);
    93 static int retcode;
    94 int * test_1_svc(char ** arg, struct svc_req * rqstp)
    95 {
    96 retcode = test(*arg);
    97 return &retcode;
    98 }

    生成的两个C文件中有部分是自己添加的。

    Makefile

    all: test_clnt test_svc

    USER_MARCOS := DEBUG
    CFLAGS := -g -static -Wall
    CC := gcc

    SRCCLNT := test_clnt.c test_local.c
    SRCSVC := test_svc.c test_remote.c

    OBJCLNT := $(patsubst %.c,%.o,$(SRCCLNT))
    OBJSVC := $(patsubst %.c,%.o,$(SRCSVC))

    %.o:%.c
    $(CC) $(CFLAGS) $(addprefix -D,$(USER_MARCOS)) -c $< -o $@

    test_clnt: ${OBJCLNT}
    $(CC) $(CFLAGS) -o test_clnt $(OBJCLNT)
    test_svc: ${OBJSVC}
    $(CC) $(CFLAGS) -o test_svc $(OBJSVC)

    clean:
    rm -rf test_clnt test_svc $(OBJCLNT) $(OBJSVC)

    使用

    root下运行

    ./test_svc

    普通用户下运行

    ./test_clnt localhost即可

    功能是计算字符串长度。






  • 相关阅读:
    检测mysq组复制的脚本
    centos7安装NFS
    mysql组复制安装
    springboot+VUE(一)
    redis集群配置
    codevs 3139 栈练习3
    codevs 3138 栈练习2
    codevs 2622 数字序列
    codevs 1054 电梯
    codevs 1507 酒厂选址
  • 原文地址:https://www.cnblogs.com/tzhangofseu/p/2247318.html
Copyright © 2020-2023  润新知