Showing posts with label Client. Show all posts
Showing posts with label Client. Show all posts

C Program to Client That Calls The Remote Procedure

Write a C Program to Client That Calls The Remote Procedure ?


Solution:

//La Spada Cristina 0000669010


/* RPC_Client.c: client che chiama la procedura remota */

#include <stdio.h>
#include <rpc/rpc.h>
#include "RPC_xFile.h"

#define COMMAND_SIZE 20

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

CLIENT *cl;
int num;
char *server, *stringa_op1;
char buffer[COMMAND_SIZE], c;
int *risultato_op2; //serve come risultato della funzione in .x che mi restituisce un int

Struttura1 *struttura1;

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


cl = clnt_create(server, FILEX, FILEVERS, "udp");

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

stringa_op1 = (char *)malloc(strlen(buffer) + 1);

do{
printf("\nChe operazione vuoi fare? (A:visualizza dettagli volo, B: elimina volo o uscire) "); 
gets(buffer);


if (buffer == NULL) {
break;
}
else if (strcmp(buffer, "A") == 0) {
printf("inserire id volo: \n");
gets(stringa_op1);
struttura1 = visualizza_dati_1(&stringa_op1, cl); 

if (struttura1->error == -1)
{
printf("l'id non esiste\n");
exit(3);
}
else
{
printf("Trovato volo: \nid: %s\nnumero: %s\norario: %s\npasseggero: %s\ngate: %s\n\n", struttura1->id, struttura1->numero, struttura1->ora, struttura1->nome, struttura1->gate);
}//operazione1

else if (strcmp(buffer, "A") == 0) {
printf("Inserisci id volo da cancellare: ");
gets(stringa_op1);

risultato_op2 = elimina_volo_1(&stringa_op1, cl);
if (risultato_op2== -1)
{
printf("errore nella eliminazione\n");
exit(4);

}
else
{
printf("Eliminazione effettuata correttamente\n");
}
}//operazione2
}while(buffer != NULL && strcmp(buffer, "uscire") != 0);


// Libero le risorse distruggendo il gestore di trasporto
free(stringa_op1);

clnt_destroy(cl);
printf("\nRPC_Client Termino...\n");
exit(0);
}

Client/Server C program to make client send the name of a file

Using TCP or IP sockets write a client/server C program to make client send the name of a file and server to send back the contents of the requested file if present.


Solution:

Client Side:

#include<stdio.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<netdb.h>
int main(int argc,char *argv[])
{
int sockfd,newsockfd,portno,len,n;
char buffer[256],c[20000];
struct sockaddr_in serv,cli;
FILE *fd;
if(argc<2)
{
printf("Err:no port no.\nusage:\n./client portno\n ex:./client 7777\n");
exit(1);
}
sockfd=socket(AF_INET,SOCK_STREAM,0);
bzero((char *)&serv,sizeof(serv));
portno=atoi(argv[1]);
serv.sin_family=AF_INET;
serv.sin_port=htons(portno);
if(connect(sockfd,(struct sockaddr *)&serv,sizeof(serv))<0)
{
printf("server not responding..\n\n\n\ti am to terminate\n");
exit(1);
}
printf("Enter the file with complete path\n");
scanf("%s",&buffer);
if(write(sockfd,buffer,strlen(buffer))<0)
printf("Err writing to socket..\n");
bzero(c,2000);
printf("Reading..\n..\n");
if(read(sockfd,c,1999)<0)
printf("error: read error\n");
printf("client: display content of %s\n..\n",buffer);
fputs(c,stdout);
printf("\n..\n");
return 0;
}

Server Side:

#include<stdio.h>
#include<sys/types.h>
#include<netinet/in.h>
#include<sys/socket.h>
#include<netdb.h>
int main(int argc,char *argv[])
{
int sockfd,newsockfd,portno,len,n;
char buffer[256],c[2000],cc[20000];
struct sockaddr_in serv,cli;
FILE *fd;
if(argc<2)
{
printf("erroe:no port no\n usage:\n/server port no\n");
exit(1);
}
sockfd=socket(AF_INET,SOCK_STREAM,0);
portno=atoi(argv[1]);
serv.sin_family=AF_INET;
serv.sin_addr.s_addr=INADDR_ANY;
serv.sin_port=htons(portno);
bind(sockfd,(struct sockaddr *)&serv,sizeof(serv));
listen(sockfd,10);
len=sizeof(cli);
printf("serve:\nwaiting for connection\n");
newsockfd=accept(sockfd,(struct sockaddr *)&cli,&len);
bzero(buffer,255);
n=read(newsockfd,buffer,255);
printf("\nserver recv:%s\n",buffer);
if((fd=fopen(buffer,"r"))!=NULL)
{
printf("server:%s found\n opening and reading..\n",buffer);
printf("reading..\n..reading complete");
fgets(cc,2000,fd);
while(!feof(fd))
{
fgets(c,2000,fd);
strcat(cc,c);
}
n=write(newsockfd,cc,strlen(cc));
if(n<0)
printf("error writing to socket");
printf("\ntransfer complete\n");
}
else
{
printf("server:file not found\n");
n=write(newsockfd,"file not foung",15);
if(n<0)
printf("error: writing to socket..\n");
}
return 0;
}


Output Using TCP or IP sockets write a client/server C program to make client send the name of a file and server to send back the contents of the requested file if present.:

Server part output


[root@localhost ~]# ./a.out 4455
server online
waiting for request...
server:(null) found!
transfering the contentserver:transfer completed
Segmentation fault
[root@localhost ~]#

client part output

[root@localhost ~]# ./a.out 4455
waiting for serverserver is online
client enter the path2.c
File Recieved:Display Contents#include<stdio.h>
struct frame{
int fslno;
char data[5];
}arr[10];
sort(int n)
{
struct frame temp;
int i,j,ex;
for(i=0;i<n-1;i++)
{
for(j=0;j<n-i-1;j++)
if(arr[j].fslno>arr[j+1].fslno)
{
temp=arr[j];
ar