/* Mike Cook
6/26/98
REU 1998
RPC Client/Server Program -- Client side
*/
#include "header.h"
struct varintarr input_structure;
struct varchararr text_input_structure, text_result_structure;
int intsRead[MAXSIZE], size;
char command;
void get_nums(register FILE *fp);
void get_nums_as_text(register FILE *fp);
void main(int argc, char **argv)
{
int ret_value, result = 0, flag = 1;
static char choice[3];
command = 'c';
printf("\n");
while (flag == 1)
{
printf("To add integers in text format, enter \'t\'.");
printf("\nTo add integers in binary format, enter \'b\'.\n");
fgets(choice, 3, stdin);
if (choice[0] != 't' && choice[0] != 'T' && choice[0] != 'b' &&
choice[0] != 'B')
{
printf("\nPlease enter a valid character.\n");
flag = 1;
}
else
flag = 0;
}
if (choice[0] == 't' || choice[0] == 'T')
printf("\nYou have selected to enter integers as text.\n");
else
printf("\nYou have selected to enter integers as binary.\n");
if ( (ret_value = callrpc(argv[1], PROGRAM, VERSION, ROUTINE_1,
xdr_char, &choice[0], xdr_void, 0)) != 0)
{
printf("callrpc error: ret_value = %d\n", ret_value);
exit(1);
}
printf("Enter some integers to add (\'q\' to quit): \n\n");
while (command != 'q')
{
if (choice[0] == 'b' || choice[0] == 'B')
{
get_nums(stdin);
if( (ret_value = callrpc(argv[1], PROGRAM, VERSION, ROUTINE_2,
xdr_varintarr, &input_structure, xdr_int, &result)) != 0)
{
printf("callrpc error: ret_value = %d\n", ret_value);
exit(1);
}
printf("\nThe sum of the integers entered is: %d\n", result);
printf("\n");
result = 0;
}
else
{
get_nums_as_text(stdin);
if( (ret_value = callrpc(argv[1], PROGRAM, VERSION, ROUTINE_3,
xdr_varchararr, &text_input_structure, xdr_varchararr,
&text_result_structure)) != 0)
{
printf("callrpc error: ret_value = %d\n", ret_value);
exit(1);
}
printf("\nThe sum of the integers entered is: %s\n",
text_result_structure.data);
printf("\n");
free(text_result_structure.data);
}
printf("Enter some more integers to add (\'q\' to quit): \n\n");
}
}
void get_nums(register FILE *fp)
{
int n, i, spaces;
char inputline[MAXLINE];
char *ptr;
fgets(inputline, MAXLINE, fp);
spaces = 0;
size = 0;
ptr = inputline;
n = strlen(inputline);
while (n < 2)
{
printf("Don\'t just hit return. Enter some integers!\n");
n = strlen(fgets(inputline, MAXLINE, fp));
}
if (inputline[0] == 'q' || inputline[0] == 'Q')
{
printf("\nYou have quit the program. Goodbye!!!!!!!!!!!\n\n");
command = 'q';
exit(0);
}
for (i = 0; i < n; i++)
{
if (inputline[i] == ' ')
spaces++;
}
spaces++; /* The number of integers on the line read */
size = (spaces + 1);
intsRead[0] = spaces;
for (i = 1; i <= spaces; i++)
{
if (i == spaces)
{
sscanf(ptr, "%d", &intsRead[i]);
}
else
{
sscanf(ptr, "%d ", &intsRead[i]);
while (*ptr != ' ')
{
ptr++;
}
ptr++;
}
}
if (ferror(fp))
perror("str_cli: error reading file");
input_structure.data = intsRead;
input_structure.arrlnth = size;
}
void get_nums_as_text(register FILE *fp)
{
int n, i, spaces;
static char inputline[MAXLINE];
char *ptr;
fgets(inputline, MAXLINE, fp);
spaces = 0;
size = 0;
ptr = inputline;
n = strlen(inputline);
while (n < 2)
{
printf("Don\'t just hit return. Enter some integers!\n");
n = strlen(fgets(inputline, MAXLINE, fp));
}
if (inputline[0] == 'q' || inputline[0] == 'Q')
{
printf("\nYou have quit the program. Goodbye!!!!!!!!!!!\n\n");
command = 'q';
exit(0);
}
if (ferror(fp))
perror("str_cli: error reading file");
text_input_structure.data = inputline;
text_input_structure.arrlnth = n;
}
©1998 MRC