Back to RPC

[RPC Sample]Converting Local Procedures to Remote Procedures

1. Single Process Version

1.1. printmsg.c

/* printmsg.c: print a message on the console */

#include <stdio.h>

int printmessage(char *msg);

int main(int argc, char const *argv[])
{
        char *message;

        if (argc != 2) {
                fprintf(stderr, "usage: %s <message>\n",
                        argv[0]);
                return 1;
        }
        message = (char *)argv[1];
        printmessage(message);
        printf("Message Delivered!\n");
        return 0;
}

int printmessage(char *msg)
{
        printf("Console:%s\n", msg);
        return 0;
}

1.2. Compile&Run

$ cc printmsg.c -o printmsg
$ printmsg "Hello, there."
Console:Hello, there.
Message delivered!

2. RPC Version


2.1. msg.x

/* msg.x: Remote msg printing protocol */
program MESSAGEPROG {
        version PRINTMESSAGEVERS {
                int PRINTMESSAGE(string) = 1;
        } = 1;
} = 0x20000001;

2.2. msg_proc.c

/*
 * msg_proc.c: implementation of the
 * remote procedure "printmessage"
 */
#include <stdio.h>
#include "msg.h" //msg.h generated by rpcgen
 
int * printmessage_1_svc(char **msg, struct svc_req *req) {
        // must be static! 
        static int result;                      
        printf("Console:%s\n", *msg);
        result = 0;
        return (&result);
}

2.3. rprintmsg.c(Clinet)


2.4. Compile&Run

$ rpcgen -C msg.x
$ cc rprintmsg.c msg_clnt.c -o rprintmsg
$ cc msg_proc.c msg_svc.c -o msg_server

2.5. Makefile

CLNT = rprintmsg
SRVR = msg_svc
CFLAGS = -g -Wall

XDR=msg
CLT_SRC=rprintmsg.c
SVC_SRC=$(XDR)_proc.c

RPCGEN = $(XDR).h $(XDR)_xdr.c $(XDR)_clnt.c $(XDR)_svc.c
# executable
CLT=$(XDR)
SVC=$(XDR)_svc

default: init build 

init:
        rpcgen -C $(XDR).x

build: $(CLNT) $(SRVR)


$(CLNT):
        cc $(CLT_SRC) $(XDR)_clnt.c -o $(CLT)

$(SRVR):
        cc $(SVC_SRC) $(XDR)_svc.c -o $(SRVR)

clean:
        rm $(CLT) $(SVC)
        rm $(RPCGEN)

3. Reference

MainWiki: rpc_sample_printmsg (last edited 2014-09-16 17:12:57 by twotwo)