Now the server code is complete. When it is compiled and executed, it makes the six RPC functions to be callable from anywhere in a network. A programmer can make use use of these functions and write whatever applications he/she wants. Given the available RPC functions, we can easily write a file transfer client program which supports GET, PUT, LS and CD commands.
The get_file() function, which reads a remote file src and saves it in file local, is listed below,
int get_file(char *src, char *local) { rFILE *fp = 0; FILE *localfp; char buf[1024]; int cnt; localfp = fopen(local, "w"); if (!localfp) { perror(local); return -1; } fp = rfopen(src, "r"); if (fp == 0) { fprintf(stderr, "Fail to open remote file!\n"); fclose(localfp); return -1; } while ((cnt = rfread(buf, 1, 1024, fp)) > 0) { fwrite(buf, 1, cnt, localfp); } fclose(localfp); rfclose(fp); free(fp); return 0; }
The code above is almost exactly what one would do to copy one local file to another using the fread(), fwrite() functions. The only difference is in the free(fp) call. PowerRPC always allocates the memory for a return value of reference type, it is the caller's responsibility to free that memory.