Vous êtes sur la page 1sur 46

PRACTICAL-01

AIM: Implement echo client-server message passing application. Message sent from client should be
displayed on server and then program should terminate.
a. Write a server (TCP) C Program that opens a listening socket and waits to serve client
b. Write a client (TCP) C Program that connects with the server program knowing IP address
and port number.
c. Get the input string from console on client and send it to server, server displays the same
string.
CODE:
// Server.c
#include<stdio.h>
#include<netinet/in.h>
#include<netdb.h>
int main(int argc,char**argv)
{
int sockfd,newsockfd,clength; // Defining variables
struct sockaddr_in serv_addr,cli_addr; // A sockaddr_in is a structure containing an internet
address.
char buffer[4096]; // Declaring buffer to store the message.
sockfd=socket(AF_INET,SOCK_STREAM,0); // The socket() system call creates a new socket.
serv_addr.sin_family=AF_INET; // The first field is short sin_family , which contains a code for
the address family.
serv_addr.sin_addr.s_addr=INADDR_ANY; // This field contains the IP address of the server
machine.
serv_addr.sin_port=htons(atoi(argv[1])); // It contain the port number.
printf("\nStart");
bind(sockfd,(struct sockaddr*)&serv_addr,sizeof(serv_addr)); // The bind() system call binds host
and port number.
printf("\nListening...");
printf("\n");
listen(sockfd,5); // The listen system call allows the process to listen on the socket for connections.
clength=sizeof(cli_addr);
newsockfd=accept(sockfd,(struct sockaddr*)&cli_addr,&clength); // accept() blocks process till
client connects to the server.
printf("\nAccepted");
printf("\n");
read(newsockfd,buffer,4096); // reading the content from the client socket.
printf("\nClient message:%s",buffer); // printing the buffer content message recieved from client.
write(newsockfd,buffer,4096); // writing content of client socket to buffer.
printf("\n");
close(sockfd); // Closing the socket.
return 0;
}

1
//Client.c

#include<stdio.h>
#include<sys/types.h>
#include<sys/socket.h> // The header file includes a number of definitions of structures needed for
sockets.
#include<netinet/in.h>
#include<netdb.h>

// To run this program please run server first and then run the client.
// In first terminal type gcc server.c
// Then ./a.out 1100(any port number)
// Then run client on other terminal type gcc client.c
// Then ./a.out client 127.0.0.1(any ip address) 1100(any port number but same as server)

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


{
int sockfd;
struct sockaddr_in serv_addr; // A sockaddr_in is a structure containing an internet address.
char buffer[4096];
sockfd=socket(AF_INET,SOCK_STREAM,0); // The socket() system call creates a new socket.
serv_addr.sin_family=AF_INET; // The first field is short sin_family , which contains a code for
the address family.
serv_addr.sin_addr.s_addr=inet_addr(argv[1]); // This field contains the IP address of the server
machine.
serv_addr.sin_port=htons(atoi(argv[2])); // It contain the port number.
printf("\nReady for sending...");
connect(sockfd,(struct sockaddr*)&serv_addr,sizeof(serv_addr)); // The connect function
establishes a connection to the server.
printf("\nEnter the message to send\n");
printf("\nClient: ");
fgets(buffer,4096,stdin); // Message is stored in the buffer.
write(sockfd,buffer,4096); // Message is sent to the client.
printf("\n");
close(sockfd); // Closing the socket.
return 0;
}

2
OUTPUT:

3
4
PRACTICAL-02
AIM: Simulate RPC (Create all the following procedures on remote machine and call it from local
machine). Write a menu-driven program for user input at client side.
List of programs for RPC:
i. Find out the factorial of given number.
ii. Implement Calculator (Basic operation).
iii. Find out whether given number is Prime Number or not.
iv. Print out the Fibonacci series till the given number
v. String is palindrome or not.
vi. Find out given year is Lear Year or not.
vii. Find out the GCD of given number.
viii. Find out the Square root of given number.
ix. Swap two variables without using 3rd variable.
x. Calculate Maximum, Minimum, average of given array
xi. Compare the given two strings.
xii. Find out whether given string is substring or not.
xiii. Concatenate the two strings.
SOLUTION:
i. Factorial
/* fact.x */
struct fact_input {
int n;
};
struct fact_output {
long int fact;
};
program FACT_PROG {
version FACT_VERS {
fact_output FACTPROC(fact_input) = 1; /* procedure number = 1 */
} = 1;
} = 0x31230000;
/* version number */
/* program number */

/* fact_server.c */
/*
* This is sample code generated by rpcgen.
* These are only templates and you can use them
* as a guideline for developing your own functions.
*/

#include "fact.h"

fact_output *

5
factproc_1_svc(fact_input *argp, struct svc_req *rqstp)
{
static fact_output result;
/*
* insert server code here
*/
long int ans = 1;
int temp = argp->n, i;
int x = argp->n;
if(temp ==0 || temp==1)
ans = 1;
else
{
for(i=1;i<=x;i++)
{
ans = ans * i;
}
}
result.fact = ans;
return &result;
}

/* fact_client.c */
/*
* This is sample code generated by rpcgen.
* These are only templates and you can use them
* as a guideline for developing your own functions.
*/

#include "fact.h"

void fact_prog_1(char *host)


{
CLIENT *clnt;
fact_output *result_1;
fact_input factproc_1_arg;

#ifndef DEBUG
clnt = clnt_create (host, FACT_PROG, FACT_VERS, "udp");
if (clnt == NULL) {
clnt_pcreateerror (host);
exit (1);
}
#endif /* DEBUG */
printf("Enter 1st no:\n");
scanf("%d",&factproc_1_arg.n);

result_1 = factproc_1(&factproc_1_arg, clnt);


if (result_1 == (fact_output *) NULL) {
6
clnt_perror (clnt, "call failed");
}
#ifndef DEBUG
printf("Factorial of %d is %ld\n",factproc_1_arg.n, result_1->fact);

clnt_destroy (clnt);
#endif /* DEBUG */
}

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

if (argc < 2) {
printf ("usage: %s server_host\n", argv[0]);
exit (1);
}
host = argv[1];
fact_prog_1 (host);
exit (0);
}

OUTPUT:

7
8
ii. Calculator
/* calc.x */
struct calc_in
{
double arg1;
double arg2;
char operator;
};
typedef double calc_out;

program CALC_PROG {
version CALC_VERS {
calc_out CALC_PROC(calc_in) = 1 ;
} = 1; /* Version# = 1 */
} = 0x3434000; /* Program# = 0x3434000 */

/* calc_server.c */
#include"calc.h"
calc_out *calc_proc_1_svc(calc_in *in, struct svc_req *rqstp) {
static calc_out out;
printf("Hello World");
switch(in->operator)
{
case 1:
out=in->arg1 + in->arg2;
break;
case 2:
out=in->arg1 - in->arg2;
break;
case 3:
out=in->arg1 * in->arg2;
break;
case 4:
out=in->arg1 / in->arg2;
break;
}
return(&out);
}

9
/* calc_client.c */
#include"calc.h"
void main(int argc, char **argv) {
CLIENT *cl;
calc_in in;
calc_out *out;

printf("\n1. Addition");
printf("\n2. Subtraction");
printf("\n3. Multiplication");
printf("\n4. Division");
printf("\n\nEnter Your Choice:");
scanf("%d",&in.operator);

printf("\n Enter 1st operator:");


scanf("%lf",&in.arg1);
printf("\n Enter 2nd operator:");
scanf("%lf",&in.arg2);
cl=clnt_create(argv[1],CALC_PROG,CALC_VERS,"tcp");
out=calc_proc_1(&in, cl);
if(out==NULL) {printf("Error: %s\n", clnt_sperror(cl,argv[1])); }
else {
printf("The result is: %lf\n",*out);
}
clnt_destroy(cl);
}

10
OUTPUT:

11
iii. Prime Number
/* prime.x */
struct prime_input{
int a;
};

struct prime_output{
int res;
};

program PRIME_PROG{
version PRIME_VERS{
prime_output PRIMEPROC(prime_input)=1;
}=1;
}=0x30200000;

/* prime_server.c */
/*
* This is sample code generated by rpcgen.
* These are only templates and you can use them
* as a guideline for developing your own functions.
*/

#include "prime.h"

prime_output *
primeproc_1_svc(prime_input *argp, struct svc_req *rqstp)
{
static prime_output result;

/*
* insert server code here
*/
int i,count=0;
int num=argp->a;
for(i=1;i<num;i++)
{
if(num%i==0)
count++;
}
if(count==1)
result.res=1;
else
result.res=0;

12
return &result;
}

/* prime_client.c */
/*
* This is sample code generated by rpcgen.
* These are only templates and you can use them
* as a guideline for developing your own functions.
*/

#include "prime.h"

void prime_prog_1(char *host)


{
CLIENT *clnt;
prime_output *result_1;
prime_input primeproc_1_arg;

#ifndef DEBUG
clnt = clnt_create (host, PRIME_PROG, PRIME_VERS, "udp");
if (clnt == NULL) {
clnt_pcreateerror (host);
exit (1);
}
#endif /* DEBUG */
printf("Please Enter a number:");
scanf("%d",&primeproc_1_arg.a);
result_1 = primeproc_1(&primeproc_1_arg, clnt);
if (result_1 == (prime_output *) NULL) {
clnt_perror (clnt, "call failed");
}
#ifndef DEBUG
if(result_1->res==0)
printf("The entered number is not prime");
else
printf("The entered number is prime");
clnt_destroy (clnt);
#endif /* DEBUG */
}

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


{
char *host;
if (argc < 2) {
printf ("usage: %s server_host\n", argv[0]);
exit (1);
}
13
host = argv[1];
prime_prog_1 (host);
exit (0);
}

OUTPUT:

14
iv. Fibonacci
/* fibo.x */
struct fibo_input{
int a;
};

struct fibo_output{
int b[100];
};

program FIBO_PROG{
version FIBO_VERS{
fibo_output FIBOPROC(fibo_input) = 1;
}=1;
}=0x32230000;

/* fibo_server.c */
/*
* This is sample code generated by rpcgen.
* These are only templates and you can use them
* as a guideline for developing your own functions.
*/

#include "fibo.h"

fibo_output *
fiboproc_1_svc(fibo_input *argp, struct svc_req *rqstp)
{
static fibo_output result;

/*
* insert server code here
*/
int c,first=0,second=1;
for ( c = 0 ; c < (argp->a) ; c++ )
{
if ( c <= 1 )
result.b[c] = c;
else
{
result.b[c] = first + second;
first = second;
second = result.b[c];
}

}
return &result;
}

15
/* fibo_client.c */
/*
* This is sample code generated by rpcgen.
* These are only templates and you can use them
* as a guideline for developing your own functions.
*/

#include "fibo.h"

void
fibo_prog_1(char *host)
{
CLIENT *clnt;
fibo_output *result_1;
fibo_input fiboproc_1_arg;

#ifndef DEBUG
clnt = clnt_create (host, FIBO_PROG, FIBO_VERS, "udp");
if (clnt == NULL) {
clnt_pcreateerror (host);
exit (1);
}
#endif /* DEBUG */
printf("Enter number of terms upto which you want series:");
scanf("%d",&fiboproc_1_arg.a);
result_1 = fiboproc_1(&fiboproc_1_arg, clnt);
if (result_1 == (fibo_output *) NULL) {
clnt_perror (clnt, "call failed");
}
#ifndef DEBUG
int i;
printf("series is:\n");
for(i=0;i<(fiboproc_1_arg.a);i++)
printf("%d \t",result_1->b[i]);
clnt_destroy (clnt);
#endif /* DEBUG */
}

int
main (int argc, char *argv[])
{
char *host;
if (argc < 2) {
printf ("usage: %s server_host\n", argv[0]);
exit (1);
}
host = argv[1];
fibo_prog_1 (host);
16
exit (0);}
OUTPUT:

17
v. Palindrome
/* pal.x */
struct pal_input {
char str[50];
};

struct pal_output {
int ans;
};

program PAL_PROG {
version PAL_VERS {
pal_output PALPROC(pal_input) = 1; /* procedure number = 1 */
} = 1;
} = 0x31230000;

/* pal_server.c */
/*
* This is sample code generated by rpcgen.
* These are only templates and you can use them
* as a guideline for developing your own functions.
*/

#include "pal.h"
#include<string.h>

pal_output *
palproc_1_svc(pal_input *argp, struct svc_req *rqstp)
{
static pal_output result;
int i, length,ans;
char reverse_string[50];
length = strlen(argp->str);
for(i = length - 1; i >= 0 ; i--)
{
reverse_string[length - i - 1] = argp->str[i];
}
for(ans = 1, i = 0; i < length ; i++)
{
if (reverse_string[i] != argp->str[i])
ans = 0;
}
result.ans=ans;
return &result;
}

18
/* pal_client.c */
#include "pal.h"

void
pal_prog_1(char *host)
{
CLIENT *clnt;
pal_output *result_1;
pal_input palproc_1_arg;

#ifndef DEBUG
clnt = clnt_create (host, PAL_PROG, PAL_VERS, "udp");
if (clnt == NULL) {
clnt_pcreateerror (host);
exit (1);
}
#endif /* DEBUG */

printf("\nEnter the string:\n");


gets(palproc_1_arg.str);

result_1 = palproc_1(&palproc_1_arg, clnt);


if (result_1 == (pal_output *) NULL) {
clnt_perror (clnt, "call failed");
}
#ifndef DEBUG
if(result_1->ans==1)
printf("\nIts a Palindrome\n");
else
printf("\nIts not a palindrome\n");

clnt_destroy (clnt);
#endif /* DEBUG */
}

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

if (argc < 2) {
printf ("usage: %s server_host\n", argv[0]);
exit (1);
}
host = argv[1];
pal_prog_1 (host);
exit (0);
}
19
OUTPUT:

20
vi. Leap Year
/* leapyear.x */
struct lyear_input {
int a;
};
struct lyear_output {
int a;
};
program LYEAR_PROG {
version LYEAR_VERS {
lyear_output LYEARPROC(lyear_input) = 1; /* procedure number = 1 */
} = 1;
} = 0x31230000;
/* version number */
/* program number */

/* leapyear_server.c */
/*
* This is sample code generated by rpcgen.
* These are only templates and you can use them
* as a guideline for developing your own functions.
*/

#include "leapyear.h"

lyear_output *
lyearproc_1_svc(lyear_input *argp, struct svc_req *rqstp)
{
static lyear_output result;

if(argp->a % 4==0 && argp->a % 100 !=0 || argp->a % 400 ==0)


result.a=0;
else
result.a=1;
return &result;
}

21
/* leapyear_client.c */

#include "leapyear.h"
void
lyear_prog_1(char *host)
{
CLIENT *clnt;
lyear_output *result_1;
lyear_input lyearproc_1_arg;

#ifndef DEBUG
clnt = clnt_create (host, LYEAR_PROG, LYEAR_VERS, "udp");
if (clnt == NULL) {
clnt_pcreateerror (host);
exit (1);
}
#endif /* DEBUG */
printf("Enter the year:");
scanf("%d",&lyearproc_1_arg.a);

result_1 = lyearproc_1(&lyearproc_1_arg, clnt);


if (result_1 == (lyear_output *) NULL) {
clnt_perror (clnt, "call failed");
}
#ifndef DEBUG
if(result_1->a==0)
printf("Leap year");
else
printf("Not a leap year");
clnt_destroy (clnt);
#endif /* DEBUG */
}

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

if (argc < 2) {
printf ("usage: %s server_host\n", argv[0]);
exit (1);
}
host = argv[1];
lyear_prog_1 (host);
exit (0);
}

22
OUTPUT:

23
vii. GCD
/* gcd.x */
struct gcd_input {
int a;
int b;
};
struct gcd_output {
int sum;
};
program GCD_PROG {
version GCD_VERS {
gcd_output GCDPROC(gcd_input) = 1; /* procedure number = 1 */
} = 1;
} = 0x31230000;

/* gcd_server.c */
/*
* This is sample code generated by rpcgen.
* These are only templates and you can use them
* as a guideline for developing your own functions.
*/

#include "gcd.h"

gcd_output *
gcdproc_1_svc(gcd_input *argp, struct svc_req *rqstp)
{
static gcd_output result;

int i,ans=0;

for(i=1; i<=(argp->a) || i<=(argp->b); i++)


{
// printf("X");
if((argp->a)%i==0 && (argp->b)%i==0)
ans=i;
}

result.sum = ans;
return &result;
}

24
/* gcd_client.c */

#include "gcd.h"
Void gcd_prog_1(char *host)
{
CLIENT *clnt;
gcd_output *result_1;
gcd_input gcdproc_1_arg;

#ifndef DEBUG
clnt = clnt_create (host, GCD_PROG, GCD_VERS, "udp");
if (clnt == NULL) {
clnt_pcreateerror (host);
exit (1);
}
#endif /* DEBUG */

printf("Enter 1st no:\n");


scanf("%d",&gcdproc_1_arg.a);
printf("Enter 2nd no:\n");
scanf("%d",&gcdproc_1_arg.b);

result_1 = gcdproc_1(&gcdproc_1_arg, clnt);


if (result_1 == (gcd_output *) NULL) {
clnt_perror (clnt, "call failed");
}
#ifndef DEBUG

printf("GCD is :\t%d",result_1->sum);
printf("\n");
clnt_destroy (clnt);
#endif /* DEBUG */
}

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

if (argc < 2) {
printf ("usage: %s server_host\n", argv[0]);
exit (1);
}
host = argv[1];
gcd_prog_1 (host);
exit (0);
25
}

OUTPUT:

26
viii. Square Root
/* sqr.x */
struct sqr_input
{
float a;
};
struct sqr_output
{
float sqoot;
};

program SQR_PROG{
version SQR_VERS{
sqr_output SQRPROC(sqr_input)=1;
}=1;
}=0x31230000;

/* sqr_server.c */
/*
* This is sample code generated by rpcgen.
* These are only templates and you can use them
* as a guideline for developing your own functions.
*/
#include<math.h>
#include "sqr.h"

sqr_output *
sqrproc_1_svc(sqr_input *argp, struct svc_req *rqstp)
{
static sqr_output result;
/*
* insert server code here
*/
int prev,k = 0;
int kmax = 1000;
float s = 1;
for(k=0;k<kmax;k++)
{
prev = s;
s = (s + argp->a/s)/2;
if(prev == s)
{
break;
}
}
result.sqoot= s;
return &result;
}

27
/* sqr_client.c */
/*
* This is sample code generated by rpcgen.
* These are only templates and you can use them
* as a guideline for developing your own functions.
*/

#include "sqr.h"
void
sqr_prog_1(char *host)
{
CLIENT *clnt;
sqr_output *result_1;
sqr_input sqrproc_1_arg;

#ifndef DEBUG
clnt = clnt_create (host, SQR_PROG, SQR_VERS, "udp");
if (clnt == NULL) {
clnt_pcreateerror (host);
exit (1);
}
#endif /* DEBUG */
printf("Enter a number :");
scanf("%f",&sqrproc_1_arg.a);
result_1 = sqrproc_1(&sqrproc_1_arg, clnt);
if (result_1 == (sqr_output *) NULL) {
clnt_perror (clnt, "call failed");
}
#ifndef DEBUG
printf("square root of a number is:%f",result_1->sqoot);
clnt_destroy (clnt);
#endif /* DEBUG */
}

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

if (argc < 2) {
printf ("usage: %s server_host\n", argv[0]);
exit (1);
}
host = argv[1];
sqr_prog_1 (host);
exit (0);
}
28
OUTPUT:

29
ix. Swap two variables without using third variable
/* swap.x */
struct add_input {
float a;
float b;
};

program ADD_PROG {
version ADD_VERS {
add_input ADDPROC(add_input) = 1; /* procedure number = 1 */
} = 1;
} = 0x31230000;
/* version number */
/* program number */

/* swap_server.c */
/*
* This is sample code generated by rpcgen.
* These are only templates and you can use them
* as a guideline for developing your own functions.
*/

#include "swap.h"

add_input *
addproc_1_svc(add_input *argp, struct svc_req *rqstp)
{
static add_input result;

float c=argp->a+argp->b;
result.a=c-argp->a;
result.b=c-argp->b;
return &result;
}

/* swap_client.c */
#include "swap.h"
void add_prog_1(char *host)
{
CLIENT *clnt;
add_input *result_1;
add_input addproc_1_arg;
#ifndef DEBUG
clnt = clnt_create (host, ADD_PROG, ADD_VERS, "udp");
if (clnt == NULL) {
clnt_pcreateerror (host);
exit (1);

30
}
#endif /* DEBUG */
printf("enter 1st number:");
scanf("%f",&addproc_1_arg.a);
printf("enter 2nd number:");
scanf("%f",&addproc_1_arg.b);

result_1 = addproc_1(&addproc_1_arg, clnt);


if (result_1 == (add_input *) NULL) {
clnt_perror (clnt, "call failed");
}
#ifndef DEBUG
printf("swapped output is:");
printf("%f \t %f",result_1->a,result_1->b);
clnt_destroy (clnt);
#endif /* DEBUG */
}

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

if (argc < 2) {
printf ("usage: %s server_host\n", argv[0]);
exit (1);
}
host = argv[1];
add_prog_1 (host);
exit (0);
}

OUTPUT:

31
32
x. Maximum, Minimum, and Average from given array
/* mma.x */
struct add_input {
int array[100];
int n;
};

struct add_output {
int min;
int max;
int avg;
};

program ADD_PROG {
version ADD_VERS {
add_output ADDPROC(add_input) = 1; /* procedure number = 1 */
} = 1; /* version number */
} = 0x31230000; /* program number */

/* mma_server.c */
#include "mma.h"
add_output *
addproc_1_svc(add_input *argp, struct svc_req *rqstp)
{
static add_output result;

/*
* insert server code here
*/

int i;

result.min=argp->array[0];
result.max=argp->array[0];
result.avg=0;
for(i=0;i<argp->n;i++)
{
if(result.min>argp->array[i])
result.min=argp->array[i];

if(result.max<argp->array[i])
result.max=argp->array[i];

result.avg=result.avg+argp->array[i];
}

result.avg=(result.avg/argp->n);
return &result;

33
}
/* mma_client.c */

#include "mma.h"
Void add_prog_1(char *host)
{
CLIENT *clnt;
add_output *result_1;
add_input addproc_1_arg;
int i;
#ifndef DEBUG

printf("Enter array size:\n");


scanf("%d",&addproc_1_arg.n);
printf("Enter array element:\n");
for(i=0;i<addproc_1_arg.n;i++)
scanf("%d",&addproc_1_arg.array[i]);

clnt = clnt_create (host, ADD_PROG, ADD_VERS, "udp");


if (clnt == NULL) {
clnt_pcreateerror (host);
exit (1);
}
#endif /* DEBUG */

result_1 = addproc_1(&addproc_1_arg, clnt);


if (result_1 == (add_output *) NULL) {
clnt_perror (clnt, "call failed");
}
#ifndef DEBUG

printf("Minimum : %d\n",result_1->min);
printf("Maximum : %d\n",result_1->max);
printf("Average : %d\n",result_1->avg);

clnt_destroy (clnt);
#endif /* DEBUG */
}
Int main (int argc, char *argv[])
{
char *host;

if (argc < 2) {
printf ("usage: %s server_host\n", argv[0]);
exit (1);
}
host = argv[1];
add_prog_1 (host);
exit (0);
34
}
OUTPUT:

35
xi. String Comparison
/* cmp.x */
struct add_input {
char str[100];
char sub_str[100];
};

struct add_output {
int result;
};

program ADD_PROG {
version ADD_VERS {
add_output ADDPROC(add_input) = 1; /* procedure number = 1 */
} = 1; /* version number */
} = 0x31230000; /* program number */

/* cmp_server.c */
/*
* This is sample code generated by rpcgen.
* These are only templates and you can use them
* as a guideline for developing your own functions.
*/

#include "cmp.h"

add_output *
addproc_1_svc(add_input *argp, struct svc_req *rqstp)
{
static add_output result;

/*
* insert server code here
*/

if(strcmp(argp->str,argp->sub_str))
result.result=0;
else
result.result=1;

return &result;
}

36
/* cmp_client.c */
#include "cmp.h"
Void add_prog_1(char *host)
{
CLIENT *clnt;
add_output *result_1;
add_input addproc_1_arg;

#ifndef DEBUG
clnt = clnt_create (host, ADD_PROG, ADD_VERS, "udp");
if (clnt == NULL) {
clnt_pcreateerror (host);
exit (1);
}
#endif /* DEBUG */

printf("Enter String:\n");
scanf("%s",addproc_1_arg.str);
printf("Enter Substring:\n");
scanf("%s",addproc_1_arg.sub_str);

result_1 = addproc_1(&addproc_1_arg, clnt);


if (result_1 == (add_output *) NULL) {
clnt_perror (clnt, "call failed");
}
#ifndef DEBUG

printf("String:%s\n",addproc_1_arg.str);
printf("Substring:%s\n",addproc_1_arg.sub_str);
if(result_1->result==1)
printf("%s and %s are Equal\n",addproc_1_arg.sub_str,addproc_1_arg.str);
else if(result_1->result==0)
printf("%s and %s are not Equal\n",addproc_1_arg.sub_str,addproc_1_arg.str);

clnt_destroy (clnt);
#endif /* DEBUG */
}
Int main (int argc, char *argv[])
{
char *host;

if (argc < 2) {
printf ("usage: %s server_host\n", argv[0]);
exit (1);
}
host = argv[1];
add_prog_1 (host);
exit (0);
37
}
OUTPUT:

38
xii. Substring
/* sub_string.c */
struct add_input {
char str[100];
char sub_str[100];
};

struct add_output {
int result;
};

program ADD_PROG {
version ADD_VERS {
add_output ADDPROC(add_input) = 1; /* procedure number = 1 */
} = 1; /* version number */
} = 0x31230000; /* program number */

/* sub_string_server.c */
/*
* This is sample code generated by rpcgen.
* These are only templates and you can use them
* as a guideline for developing your own functions.
*/

#include "sub_string.h"

add_output *
addproc_1_svc(add_input *argp, struct svc_req *rqstp)
{
static add_output result;

/*
* insert server code here
*/

if(strstr(argp->str,argp->sub_str))
result.result=1;
else
result.result=0;

return &result;
}

39
/* sub_string_client.c */
#include "sub_string.h"
Void add_prog_1(char *host)
{
CLIENT *clnt;
add_output *result_1;
add_input addproc_1_arg;
#ifndef DEBUG
clnt = clnt_create (host, ADD_PROG, ADD_VERS, "udp");
if (clnt == NULL) {
clnt_pcreateerror (host);
exit (1);
}
#endif /* DEBUG */

printf("Enter String:\n");
scanf("%s",addproc_1_arg.str);
printf("Enter Substring:\n");
scanf("%s",addproc_1_arg.sub_str);

result_1 = addproc_1(&addproc_1_arg, clnt);


if (result_1 == (add_output *) NULL) {
clnt_perror (clnt, "call failed");
}
#ifndef DEBUG
printf("String:%s\n",addproc_1_arg.str);
printf("Substring:%s\n",addproc_1_arg.sub_str);
if(result_1->result==1)
printf("%s is substring of %s\n",addproc_1_arg.sub_str,addproc_1_arg.str);
else if(result_1->result==0)
printf("%s is not substring of %s\n",addproc_1_arg.sub_str,addproc_1_arg.str);
clnt_destroy (clnt);
#endif /* DEBUG */
}

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

if (argc < 2) {
printf ("usage: %s server_host\n", argv[0]);
exit (1);
}
host = argv[1];
add_prog_1 (host);
exit (0);
}

40
OUTPUT:

41
xiii. String Concatenate
/* con.x */
struct add_input {
char str[100];
char sub_str[100];
};

struct add_output {
int result;
};

program ADD_PROG {
version ADD_VERS {
add_output ADDPROC(add_input) = 1; /* procedure number = 1 */
} = 1; /* version number */
} = 0x31230000; /* program number */

/* con_server.c */
/*
* This is sample code generated by rpcgen.
* These are only templates and you can use them
* as a guideline for developing your own functions.
*/

#include "con.h"
#include<string.h>
add_output *
addproc_1_svc(add_input *argp, struct svc_req *rqstp)
{
static add_output result;

strcat(argp->a,argp->b);
strcpy(result.c,argp->a);

return &result;
}

42
/* con_client.c */

#include "con.h"

void
add_prog_1(char *host)
{
CLIENT *clnt;
add_output *result_1;
add_input addproc_1_arg;

#ifndef DEBUG
clnt = clnt_create (host, ADD_PROG, ADD_VERS, "udp");
if (clnt == NULL) {
clnt_pcreateerror (host);
exit (1);
}
#endif /* DEBUG */
printf("Enter string 1: \n");
scanf("%s",addproc_1_arg.a);
printf("Enter string 2: \n");
scanf("%s",addproc_1_arg.b);
result_1 = addproc_1(&addproc_1_arg, clnt);
if (result_1 == (add_output *) NULL) {
clnt_perror (clnt, "call failed");
}
#ifndef DEBUG
printf("\nconcated string is %s:",result_1->c);
clnt_destroy (clnt);
#endif /* DEBUG */
}

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

if (argc < 2) {
printf ("usage: %s server_host\n", argv[0]);
exit (1);
}
host = argv[1];
add_prog_1 (host);
exit (0);
}

43
OUTPUT:

44
PRACTICAL-03
AIM: Implement RMI such that remote method returns the sum of two floating point numbers
to the client process.

CODE:
/*addclient.java*/
import java.rmi.*;
import java.lang.Double.*;
public class addclient
{
public static void main(String args[])
{
try
{
String addserverurl="rmi://"+args[0]+"/addserver";
addserverinf asinf=(addserverinf)Naming.lookup(addserverurl);
System.out.println("The first number is:"+args[1]);
//Double d1=Double.ValueOf(args[1].doubleValue());
Double d1=Double.parseDouble(args[1]);
System.out.println("The second number is:"+args[2]);
//Double d2=Double.ValueOf(args[2].doubleValue());
Double d2=Double.parseDouble(args[2]);
System.out.println("the sum is:"+asinf.add(d1,d2));
}
catch(Exception e)
{
System.out.println("Exception:"+e);
}
}
}
/*addserver.java*/
import java.net.*;
import java.rmi.*;
public class addserver
{
public static void main(String args[])
{
try
{
addserverimpl asi=new addserverimpl( );
Naming.rebind("addserver",asi);
}
catch(Exception e)
{
System.out.println("Exception:"+e);
}
}

45
}

/*addserverimpl.java*/
import java.rmi.*;
import java.rmi.server.*;
public class addserverimpl extends UnicastRemoteObject implements addserverinf
{
public addserverimpl( ) throws RemoteException
{

}
public double add(double d1,double d2) throws RemoteException
{
return d1+d2;
}
}
/*addserverinf.java*/
import java.rmi.*;
public interface addserverinf extends Remote
{
double add(double d1,double d2) throws RemoteException;

OUTPUT:

46

Vous aimerez peut-être aussi