当前访客身份:游客 [ 登录 | 加入 OSCHINA ]

代码分享

当前位置:
代码分享 » C/C++  » 网络编程
红薯

Windows与Linux的RPC通信源码

红薯 发布于 2010年09月28日 6时, 3评/4453阅
分享到: 
收藏 +0
1
<无详细内容>

代码片段(2) [全屏查看所有代码]

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);
}


开源中国-程序员在线工具:Git代码托管 API文档大全(120+) JS在线编辑演示 二维码 更多»

发表评论 回到顶部 网友评论(3)

  • 1楼:hmmuestc 发表于 2011-12-04 15:26 回复此评论
    请问windows 端的如何编译呢?
  • 2楼:戚继光 发表于 2012-12-26 14:37 回复此评论
    好帖前留名纪念一下。
  • 3楼:戚继光 发表于 2013-01-06 11:39 回复此评论
    是不是缺少"file.h"这个头文件???
开源从代码分享开始 分享代码