Здравейте,
трябва да реализирам отдалечена процедура за работа със свързан списък. В момента се опитвам да реализиран най-лесното, а именно да добавям елемент в свързания списък.
При първо извикване на сървърската функция се въвеждат елементите и се изписват на екрана, но като я извикам пак тези елементи вече не съществуват (или списъка вече е празен), а се заменят с нововъведените.
Ако някой може да помогне с това, ще му бъда благодарен, тъй като запецнах тука, пък трябва после да изтривам и т.н.
Прилагам: файловете.
llist.x
struct list {
int key;
list *next;
};
program PRINTER {
version PRINTER_V1 {
int PRINT_LIST(list) = 1;
int SUM_LIST(list) = 2;
} = 1;
} = 0x2fffffff;
llist.c
#include <stdio.h>
#include <stdlib.h>
//#include <conio.h>
#include "llist.h"
/*list *mk_list(int key)
{
list *lst;
lst = (list*)malloc(sizeof(list));
if (lst == NULL)
return NULL;
lst->key = key;
lst->next = NULL;
return lst;
}
*/
int main(int argc, char *argv[])
{
list *l;
CLIENT *cl;
int *result;
l = (list*)malloc(sizeof(list));
//l = NULL;
if (argc < 2)
return 1;
////////////////////////////////////////
char ch;
do {
printf("\n RPC with linked list ");
printf("\n");
printf("\n 1.Add new element ");
printf("\n 2.Delete element ");
printf("\n 3.Change priority ");
printf("\n 5.Quit ");
printf("\n");
ch = getchar();
printf("\n");
switch(ch) {
case '1':
cl = clnt_create(argv[1], PRINTER, PRINTER_V1, "tcp");
if (cl == NULL) {
printf("error: could not connect to server.\n");
return 1;
}
result = print_list_1(l, cl);
if (result == NULL) {
printf("error: RPC failed!\n");
return 1;
}
printf("client: server says it printed %d items.\n", *result);
break;
case '2':
printf("b ");
break;
case '3':
printf("c");
break;
case '4':
printf("d ");
break;
default:
printf("Choose an option from the menu");
printf("\n");
break;
}
} while(ch != '5');
return 0;
}
////////////////////////////////////////////////
/* l = new = mk_list("one", 1, ORANGE);
new = mk_list("two", 2, TURQUOISE);
new->next = l; l = new;
new = mk_list("three", 3, ORANGE);
new->next = l; l = new;
cl = clnt_create(argv[1], PRINTER, PRINTER_V1, "tcp");
if (cl == NULL) {
printf("error: could not connect to server.\n");
return 1;
}
result = print_list_1(l, cl);
if (result == NULL) {
printf("error: RPC failed!\n");
return 1;
}
printf("client: server says it printed %d items.\n", *result);
return 0;
} */
llist_svc_proc.c
#include <stdio.h>
#include <stdlib.h>
#include "llist.h"
int result;
/* print out a list, returning the number of items printed */
int *print_list_1_svc(list *lst, struct svc_req *req)
{
if (lst==NULL) printf("fuck");
list * temp;
list * ptr, *head;
head=NULL;
int i,p,z;
printf ("How many elements:");
scanf ("%ld",&i);
for(p=1;p<=i;p++) {
//create a temporary node
temp = (list*)malloc(sizeof(list)); //allocate space for node
printf ("element:");
scanf ("%ld",&z);
temp->key = z; // store data(first field)
temp->next=head; // store the address of the pointer head(second field)
head = temp; // transfer the address of 'temp' to 'head'
}
ptr=head;
result = 0;
while (ptr != NULL) {
printf("%d", ptr->key);
printf("\n");
result++;
ptr = ptr->next;
}
return &result;
}
int *sum_list_1_svc(list *lst, struct svc_req *req)
{
list *ptr;
ptr = lst;
result = 0;
while (ptr != NULL) {
printf("%d", ptr->key);
printf("\n");
result++;
ptr = ptr->next;
}
return &result;
}