Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

Ujjwal Singh

0801IT211090
LAB ASSIGNMENT 5

Q. Write and test a Sample “Hello World” RPC application in a procedural language such as C.
There should be a remote procedural call which should display “Hello World&” as the
output.
CODE:
hello.x
program HELLOPROG {
version HELLOVERSION {
string say_hello() = 1;
} = 1;
} = 0x31234567;

hello_server.c (RPC Server)


#include <stdio.h>
#include <rpc/rpc.h>
#include "hello.h" // Generated by rpcgen

char* say_hello_svc(void* arg, struct svc_req* req) {


static char hello[] = "Hello World";
return hello;
}

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


if (argc != 2) {
fprintf(stderr, "Usage: %s <protocol>\n", argv[0]);
exit(1);
}

if (strcmp(argv[1], "tcp") != 0 && strcmp(argv[1], "udp") != 0) {


fprintf(stderr, "Protocol must be 'tcp' or 'udp'\n");
exit(1);
}

pmap_unset(HELLOPROG, HELLOVERSION);

if (strcmp(argv[1], "tcp") == 0) {
if (!svc_create(say_hello_svc, HELLOPROG, HELLOVERSION, "tcp")) {
fprintf(stderr, "Unable to create TCP service.\n");
exit(1);
Ujjwal Singh
0801IT211090
}
} else {
if (!svc_create(say_hello_svc, HELLOPROG, HELLOVERSION, "udp")) {
fprintf(stderr, "Unable to create UDP service.\n");
exit(1);
}
}

svc_run();
fprintf(stderr, "Error: svc_run() returned\n");
exit(1);
}

hello_client.c (RPC Client)


#include <stdio.h>
#include <rpc/rpc.h>
#include "hello.h" // Generated by rpcgen

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


CLIENT* cl;
char* server;
char* result;

if (argc != 2) {
fprintf(stderr, "Usage: %s <server>\n", argv[0]);
exit(1);
}

server = argv[1];

cl = clnt_create(server, HELLOPROG, HELLOVERSION, "tcp");


if (cl == NULL) {
clnt_pcreateerror(server);
exit(1);
}

result = say_hello_1(NULL, cl);


if (result == NULL) {
clnt_perror(cl, server);
exit(1);
Ujjwal Singh
0801IT211090
}

printf("Server says: %s\n", result);

clnt_destroy(cl);
return 0;
}

OUTPUT:

You might also like