REMOTE PROCEDURE CALL (RPC) USING SOCKETS

Server program
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<unistd.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<errno.h>
int i,f;
int main()
{
int sd,acpt,port,len,n,result;
char choice[50];
struct sockaddr_in serv,cli;
if((sd=socket(AF_INET,SOCK_STREAM,0))<0)
{
printf("Error in socket\n");
exit(0);
}
bzero(&serv,sizeof(serv));
printf("Enter the port number : ");
scanf("%d",&port);
serv.sin_family=AF_INET;
serv.sin_port=htons(port);
serv.sin_addr.s_addr=htonl(INADDR_ANY);
if(bind(sd,(struct sockaddr *)&serv,sizeof(serv))<0)
{
printf("Error in bind\n");
exit(0);
}
if(listen(sd,3)<0)
{
printf("Error in listen\n");
exit(0);
}
len=sizeof(cli);
if((acpt=accept(sd,(struct sockaddr*)&cli,&len))<0)
{
printf("Error in accept");
exit(0);
}
while(1)
{
recv(acpt,&n,sizeof(n),0);
printf("\nThe number is received : %d",n);
result=fact(n);
printf("\n the factorial value is : %d\n",result);
send(acpt,&result,sizeof(result),0);
result=0;
recv(acpt,&choice,sizeof(choice),0);

if(strcmp(choice,"n")==0)
{
printf("\n*** Server exit ***\n");
break;
}

}
close(acpt);
close(sd);
return 0;
}
Client program

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<unistd.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<errno.h>
int main()
{
int sd,acpt,con,port,n,result;
char choice[50];
struct sockaddr_in serv,cli;
if((sd=socket(AF_INET,SOCK_STREAM,0))<0)
{
printf("Error in socket\n");
exit(0);
}
bzero(&serv,sizeof(serv));
printf("Enter the port number  :  ");
scanf("%d",&port);
serv.sin_family=AF_INET;
serv.sin_port=htons(port);
serv.sin_addr.s_addr=htonl(INADDR_ANY);
if(con=connect(sd,(struct sockaddr *)&serv,sizeof(serv))<0)
{
printf("Error in connection\n");
exit(0);
}
while(1)
{
printf("\nEnter the first data  :");
scanf("%d",&n);
send(sd,&n,sizeof(n),0);
recv(sd,&result,sizeof(result),0);
printf("\nThe factorial  value is :%d",result);
printf("\nDo you want continue : ");
scanf("%s",&choice);
send(sd,&choice,sizeof(choice),0);
if(strcmp(choice,"n")==0)
{
printf("\n*** Client exit ***\n");
break;
}
}
close(sd);
}

OUTPUT

SERVER

[11ca013@mcalinux network]$ cc 9rcpfactser.c
[11ca013@mcalinux network]$ ./a.out
Enter the port number : 1806

The number is received : 2
 the factorial value is : 2

The number is received : 3
 the factorial value is : 6

The number is received : 4
 the factorial value is : 24

The number is received : 5
 the factorial value is : 120

The number is received : 5
 the factorial value is : 120

The number is received : 5
 the factorial value is : 120

CLIENT

[11ca013@mcalinux network]$ cc 9rcpfactcli.c
[11ca013@mcalinux network]$ ./a.out
Enter the port number  :  1806

Enter the first data  :2

The factorial  value is :2
Do you want continue : 1

Enter the first data  :3

The factorial  value is :6
Do you want continue : 1

Enter the first data  :4

The factorial  value is :24
Do you want continue : 1

Enter the first data  :5

The factorial  value is :120

Do you want continue : 0

0 Response to "REMOTE PROCEDURE CALL (RPC) USING SOCKETS"

Post a Comment



Your IP Address is:

Browser: