Public feed
#892939
simpclient.c
/* RPC client for simple addition example */
#include <stdio.h>
#include "simp.h" /* Created for us by rpcgen - has everything we need ! */
#include <string.h>
/* Wrapper function takes care of calling the RPC procedure */
int add( CLIENT *clnt, int x, int y,char *nombres, char *text) {
operands ops;
int *result;
int longitud,i,j, long2;
char *str;
//ops.nombre=(char*)malloc(strlen(nombres));
// printf("%s algod\n", nombres);
/* Gather everything into a single data structure to send to the server */
ops.x = x;
ops.y = y;
longitud = strlen(nombres);
//printf("%d la longitud es: \n", longitud);
for(i=0; i < longitud; i++)
{
printf("%s lo q tiene str es: \n", &nombres[i]);
//str = strcat(nombres[i], &str);
//printf("%s lo q tiene str es: \n", &str);
}
long2 = strlen(text);
for(j=0; j < long2; j++)
{
printf("%s lo q tiene str es: \n", &text[j]);
//str = strcat(nombres[j], &str);
//printf("%s lo q tiene str es: \n", &str);
}
printf("%s algoe\n", nombres);
//strcpy(ops.nombre,nombres);
ops.nombre=nombres;
ops.texto = text;
printf("%s algodos\n", ops.nombre);
/* Call the client stub created by rpcgen */
result = add_1(&ops,clnt);
if (result==NULL) {
fprintf(stderr,"Trouble calling remote procedure\n");
exit(0);
}
return(*result);
}
/* Wrapper function takes care of calling the RPC procedure */
int sub( CLIENT *clnt, int x, int y,char *nombres, char *text) {
operands ops;
int *result;
/* Gather everything into a single data structure to send to the server */
ops.x = x;
ops.y = y;
ops.nombre=nombres;
ops.texto = text;
/* Call the client stub created by rpcgen */
result = sub_1(&ops,clnt);
if (result==NULL) {
fprintf(stderr,"Trouble calling remote procedure\n");
exit(0);
}
return(*result);
}
int main( int argc, char *argv[]) {
CLIENT *clnt;
int x,y;
//char *nombre;
//char pepe;
if (argc < 2) {
fprintf(stderr,"Usage: %s hostname num1 num\n",argv[0]);
exit(0);
}
else{
if (argc == 2){
printf("El formato es el siguiente: ./hola_client\n");
printf("Host donde se va a ejecutar, por ejemplo: localhost\n");
printf("El numero de operacion a realizar:\n");
printf("1 - Abrir\n");
printf("2 - Leer\n");
printf("3 - Escribir\n");
printf("4 - Borrar\n");
printf("Nombre del archivo con el que se quiere trabajar sin la extension\n");
printf("En el caso de elegir la opcion escribir ingrese el texto\n");
}
}
if (argc == 6){
switch (atoi(argv[2])){
case 1:
printf("Esta operacion abre el archivo de nombre\n");
//display_prg_1 (host, atoi(argv[2]), argv[3]);
break;
case 2:
printf("Esta operacion lee el archivo de nombre\n");
printf(argv[3]);
break;
case 3:
printf("Esta operacion Escribe el archivo de nombre\n");
printf(argv[3]);
break;
case 4:
printf("Esta operacion borra el archivo de nombre\n");
printf(argv[3]);
break;
}
}
else{
printf("Por favor, ingrese los parametros de forma correcta\n");
}
/* Create a CLIENT data structure that reference the RPC
procedure SIMP_PROG, version SIMP_VERSION running on the
host specified by the 1st command line arg. */
clnt = clnt_create(argv[1], SIMP_PROG, SIMP_VERSION, "udp");
/* Make sure the create worked */
if (clnt == (CLIENT *) NULL) {
clnt_pcreateerror(argv[1]);
exit(1);
}
/* get the 2 numbers that should be added */
x = atoi(argv[2]);
y = atoi(argv[3]);
printf("%d + %d = %d\n",x,y, add(clnt,x,y,argv[4], argv[5]));
//printf("%d - %d = %d\n",x,y, sub(clnt,x,y,nombre));
printf("%s algos \n",argv[4]);
return(0);
}
**************************************************************************
simpservice.c
/* Definition of the remote add and subtract procedure used by
simple RPC example
rpcgen will create a template for you that contains much of the code
needed in this file is you give it the "-Ss" command line arg.
*/
#include <stdio.h>
#include "simp.h"
/* Here is the actual remote procedure */
/* The return value of this procedure must be a pointer to int! */
/* we declare the variable result as static so we can return a
pointer to it */
int *
add_1_svc(operands *argp, struct svc_req *rqstp)
{
static int result;
//static char *nom;
int longitud,i;
printf("Got request: adding %d, %d\n",
argp->x, argp->y);
result = argp->x + argp->y;
//nom = argp->nombre;
/*longitud = strlen(argp->nombre);
printf("%d la longitud es: \n", longitud);
/*for(i=0; i < 3; i++)
{printf("%s lo q tiene str es: \n", &argp->nombre[i]);
//str = strcat(nombres[i], &str);
//printf("%s lo q tiene str es: \n", &str);
}
printf("%s algoe\n",argp->nombre);*/
printf("%d algo1 \n",argp->x);
printf("%d algo2 \n",argp->y);
printf("%s algo3 \n",argp->nombre);
printf("%s algo4 \n",argp->texto);
//printf("%s algo \n",argp->nombre[1]);
return (&result);
}
int *
sub_1_svc(operands *argp, struct svc_req *rqstp)
{
static int result;
printf("Got request: subtracting %d, %d\n",
argp->x, argp->y);
result = argp->x - argp->y;
return (&result);
}
**************************************************************************
simp.c
/* Intelligent Addition/Subtraction Program */
/* Copyright Dave Hollinger Enterprises, 1998 */
/* All Rights Reserved */
/* This is the core of the program, it uses advanced artificial intelligence
techniques to compute the sum and difference of 2 integers in very little
time.
These operations can be critical in any solution to the Year 2000 problem!
*/
#include <stdio.h>
#include <stdlib.h>
int add( int x, int y ) {
return(x+y); /* Note the use of the '+' operator to achieve addition! */
}
int subtract( int x, int y ) {
return(x-y); /* This is a little harder, we have to use '-' */
}
int main( int argc, char *argv[]) {
int x,y;
if (argc!=3) {
fprintf(stderr,"Usage: simp num1 num\n");
fprintf(stderr," num1 and num2 must be integer values (for now)\n");
fprintf(stderr," Floating point arithmetic is coming soon - preregister now and\n");
fprintf(stderr," receive our new integrated multiplication program at no extra cost!\n");
exit(0);
}
x = atoi(argv[1]);
y = atoi(argv[2]);
printf("%d + %d = %d\n",x,y,add(x,y));
printf("%d - %d = %d\n",x,y,subtract(x,y));
return(0);
}
**************************************************************************
simp.x
/* Protocol definition file for a simple RPC application */
/* There are 2 remote procedures - an add procedure and
a subtract procedure. Each must be called with a single
parameter, a structure that holds 2 integers. The return
value of each procedure is an int.
Notice that I can put comments in the protocol definition file!
*/
/* I can also put defines in here - and use them, although they will
not show up in the .h file produced by rpcgen.
*/
#define VERSION_NUMBER 1
/* If I want to put something explicitly in the .h file produced by rpcgen
I can start it with %:
*/
%#define foo 127
/* rpcgen just strips the '%' and puts the rest in the .h file */
/* here is the definition of the data type that will be passed to
both of my remote procedures */
struct operands {
int x;
int y;
char *texto;
char *nombre;
};
/* note that this data type will be defined in the .h file produced by
rpcgen. AND it will be typedef'd as well, so I can refer to it as
type 'operands', I don't need to use 'struct operands'.
*/
/* OK - here is the real stuff - the program, version and procedure
definitions. Remember this is not C, although it looks similar */
program SIMP_PROG {
version SIMP_VERSION {
int ADD(operands) = 1;
int SUB(operands) = 2;
} = VERSION_NUMBER;
} = 555555555;
/* This defines the RPC program number as 555555555, although within the
client and server code I can just refer to it as SIMP_PROG. In other words,
rpcgen will put this line in the .h file:
#define SIMP_PROG 555555555
The version number is 1, but I can use SIMP_VERSION.
The remote procedure numbers are 1 for the add routine and 2 for the
subtract routine, although I can use the symbolic constants ADD and SUB.
------------------------------
On the client side, a stub will be generated in simp_clnt.c for each
of the remote procedures. The stub prototype for the add procedure looks like
this:
int * add_1(operands *, CLIENT *);
the "add" part of the name comes from the ADD in the protocol
definition. the "_1" indicates the version number (if we set the
version to 100, the stub name would be "add_100").
When we call this routine in the client, this stub takes care of
all the RPC stuff for us, all we have to do is give it the address
of an operands structure with the x and y fields set, and an RPC handle
that is already established (the CLIENT * arg).
If this isn't easy enough - just do this:
rpcgen -C -Sc simp.x
the -C tells rpcgen to output ANSI C code (not K&R).
the -Sc tells rpcgen to output sample client code that makes a
call to each of the remote procedures, you can take this
code and adjust it to your needs.
---------------------------------
On the server side, rpcgen creates the file simp_svc.h that includes
a main() and an RPC dispatch routine. All we need to supply is the
actual remote procedure subroutines. The server expects these routines
to have prototypes like this:
int *add_1_svc(operands*, struct svc_req *);
int *sub_1_svc(operands*, struct svc_req *);
We need to write these routines, and include them in the link used
to create the server executable.
*/