[1].[代码] Linux服务器端源码 跳至 [1] [2]

/*
linux rpc server
author:futuresk

qq:623975927
email:wangluozhaoli@163.com
2010.9.25
*/
#include <stdlib.h>
#include <fcntl.h>
#include <sys/stat.h>
//#include <io.h>
#include <string.h>
#include "file.h"
#include <stdio.h>
static FILE *fp;
int *
file_open_1_svc(char **argp, struct svc_req *rqstp)
{
static int result=1;
if((fp=fopen(*argp,"r"))==NULL)
{
   printf("read failed!");
}
fclose(fp);
/*
* insert server code here
*/

return &result;
}

char **
file_read_1_svc(char **argp, struct svc_req *rqstp)
{
static char * result;
fp=fopen(*argp,"r");
char ch;
char buffer[4096];
ch=fgetc(fp);
int i=0;
while(ch!=EOF)
{
   buffer[i]=ch;;
   i++;
   ch=fgetc(fp);
}
/*
*
* insert server code here
*/
result=buffer;
return &result;
}

int *
file_write_1_svc(char **argp, struct svc_req *rqstp)
{
static int result;
int handle;
   char string[40];
   int length, res;
   if ((handle = open(*argp, O_WRONLY | O_CREAT | O_TRUNC,S_IREAD | S_IWRITE)) == -1)
   {
     printf("Error opening file.\n");
      exit(1);
   }
   strcpy(string, "hello world!");
   length = strlen(string);
   if ((res = write(handle, string, length)) != length)
   {
         printf("Error writing to the file.\n");
         exit(1);
   }
// printf("Wrote %d bytes to the file.\n", res);
   close(handle);
result=res;
/*
* insert server code here
*/

return &result;
}

int *
file_close_1_svc(char **argp, struct svc_req *rqstp)
{
static int result=1;
fp=fopen(*argp,"r");
fclose(fp);
/*
* insert server code here
*/

return &result;
}


//这个是rpcgen生成的函数头,自己写的函数体

[2].[代码] Windows客户端代码 跳至 [1] [2]

/*
windows client 实现与linux的rpc调用
author:futuresk
qq:623975927
email:wangluozhaoli@163.com
2010.9.25
*/
#include "file.h"
#include "file_clnt.c"
#pragma comment(lib,"pwrpc32.lib")

void
file_prog_1(char *host,char *filename)
{
CLIENT *clnt;
int *result_1;
char * *result_2;
int *result_3;
int *result_4;

#ifndef DEBUG
clnt = clnt_create (host, FILE_PROG, FILE_VERSION, "udp");
if (clnt == NULL) {
   clnt_pcreateerror (host);
   exit (1);
}
#endif /* DEBUG */
printf("open start-----------------\n");
result_1 = file_open_1(&filename, clnt);
if (result_1 == (int *) NULL) {
   clnt_perror (clnt, "call failed");
}
printf("read start-----------------\n");
result_2 = file_read_1(&filename, clnt);
if (result_2 == (char **) NULL) {
   clnt_perror (clnt, "call failed");
}
else
{
   printf("the context is:%s\n",*result_2);
}
printf("write start-----------------\n");
result_3 = file_write_1(&filename, clnt);
if (result_3 == (int *) NULL) {
   clnt_perror (clnt, "call failed");
}
else
{
   printf("write %d characters successfully!\n",*result_3);
}
printf("close start-------------------\n");
result_4 = file_close_1(&filename, clnt);
if (result_4 == (int *) NULL) {
   clnt_perror (clnt, "call failed");
}
#ifndef DEBUG
clnt_destroy (clnt);
#endif /* DEBUG */
}


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

char *filename;
if (argc < 3) {
   printf ("usage: %s server_host filename\n", argv[0]);
   exit (1);
}
host = argv[1];
filename=argv[2];
file_prog_1 (host,filename);
exit (0);
}