首页 > 其他分享 >远程过程调用:门和Sun RPC

远程过程调用:门和Sun RPC

时间:2023-12-21 10:35:56浏览次数:35  
标签:调用 struct argp Sun calculator RPC result CALCULATOR

一、门

门提供了调用同一台主机上的另外一个进程中某个过程的能力。门是一种特殊类型的IPC,因为客户端和服务器之间以函数参数和返回值形式交换信息。

示意图:

本想验证书中源码,发现头文件:#include <door.h> 都没有,互联网上查了很久,也没有相关的信息。暂且作罢。。。

二、Sun RPC

远程过程调用(Remote Procedure Call,简称RPC):被调用过程和调用过程处于不同的进程中。RPC通常允许一台主机上的某个客户调用另外一台主机上的某个服务器过程,只要这两台主机以某种形式的网络连接着。

远程过程调用可能是同步的,也可能是异步的。

示意图;

通过RPC实现客户端向服务端调用过程请求,服务器返回一个响应结果。但对于用户来说隐藏了底层网络通讯细节,仅给定服务器的ip即可。

计算器

  • 编写calculator.x文件
/* 
 * filename: calculator.x 
 * function: 定义远程调用中常量、非标准数据类型以及调用过程
 */

const ADD = 0;
const SUB = 1;
const MUL = 2;
const DIV = 3;

struct CALCULATOR
{
    int op; /* 0-ADD, 1-SUB, 2-MUL, 3-DIV */
    float arg1;
    float arg2;
    float result;
};

program CALCULATOR_PROG
{
    version CALCULATOR_VER
    {
        struct CALCULATOR CALCULATOR_PROC(struct CALCULATOR) = 1;
    } = 1;  /*  版本号=1 */
} = 0x20000001; /* RPC程序编号 */
  • rpcgen编译calculator.x文件

命令:rpcgen -a calculator.x

  • 修改文件 calculator_server.c (注意:这个文件不是用户自己创建的,而是由rpcgen编译.x文件时创建的)
/*
 * This is sample code generated by rpcgen.
 * These are only templates and you can use them
 * as a guideline for developing your own functions.
 */

#include "calculator.h"

struct CALCULATOR *
calculator_proc_1_svc(struct CALCULATOR *argp, struct svc_req *rqstp)
{
	static struct CALCULATOR  result;

	/*
	 * insert server code here
	 */
	/* -<<< Add to test*/
	switch (argp->op)
	{
	case ADD:
		result.result = argp->arg1 + argp->arg2;
		break;
	case SUB:
		result.result = argp->arg1 - argp->arg2;
		break;
	case MUL:
		result.result = argp->arg1 * argp->arg2;
		break;
	case DIV:
		result.result = argp->arg1 / argp->arg2;
		break;
	default:
		break;
	}
	/* -<<< Add to test*/

	return &result;
}

  • 修改文件 calculator_client.c (注意:这个文件不是用户自己创建的,而是由rpcgen编译.x文件时创建的)
/*
 * This is sample code generated by rpcgen.
 * These are only templates and you can use them
 * as a guideline for developing your own functions.
 */

#include "calculator.h"


void
calculator_prog_1(char *host)
{
	CLIENT *clnt;
	struct CALCULATOR  *result_1;
	struct CALCULATOR  calculator_proc_1_arg;

#ifndef	DEBUG
	clnt = clnt_create (host, CALCULATOR_PROG, CALCULATOR_VER, "udp");
	if (clnt == NULL) {
		clnt_pcreateerror (host);
		exit (1);
	}
#endif	/* DEBUG */

	/* -<<< Add to test*/
	char c;
	printf("choose the operation:\n\t0---ADD\n\t1---SUB\n\t2---MUL\n\t3---DIV\n");
	c = getchar();
	if (c > '3' && c < '0')
	{
		printf("error:operate/n");
		exit(1);
	}
	calculator_proc_1_arg.op = c - '0';
	printf("input the first number:");
	scanf("%f", &calculator_proc_1_arg.arg1);
	printf("input the second number:");
	scanf("%f", &calculator_proc_1_arg.arg2);
	/* -<<< Add to test*/

	result_1 = calculator_proc_1(&calculator_proc_1_arg, clnt);
	if (result_1 == (struct CALCULATOR *)NULL)
	{
		clnt_perror(clnt, "call failed");
	}
#ifndef DEBUG
	clnt_destroy(clnt);
#endif /* DEBUG */

	/* -<<< Add to test*/
	printf("The Result is %.3f \n", result_1->result);
	/* -<<< Add to test*/
}


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

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

  • 编译 calculator_server.c 和 calculator_client.c

由于rpcgen编译时已经创建了Makefile文件,故可直接执行:make -f Makefile.calculator

  • 将可执行文件 calculator_server 放在主机A,可执行文件 calculator_client 拷贝至主机B

在主机A上运行 calculator_server ,然后在主机B上运行** calculator_client**

三、参考引用

Linux下C实现RPC

标签:调用,struct,argp,Sun,calculator,RPC,result,CALCULATOR
From: https://www.cnblogs.com/caojun97/p/17911343.html

相关文章

  • 微服务调用链的排查,请求日志排查超时时间,锁定超时的原因
    微服务调用链的排查,请求日志排查超时时间,锁定超时的原因A微服务>>B微服务>>C微服务论日志的请求开始时间和结束时间的重要性。完整的日志格式:另外接口的入参,出参也需要加上,日志需要根据参数的关键字来搜索,比如会员号,ID等唯一标识。A服务 logger.info("调用B服务httpParam......
  • odoo rpc用法
     js /**@odoo-module**/import{registry}from"@web/core/registry";import{Layout}from"@web/search/layout";import{getDefaultConfig}from"@web/views/view";import{Component,onWillStart,useSubEnv}from&q......
  • Spring Cloud OpenFeign:基于Ribbon和Hystrix的声明式服务调用
    ......
  • Midjourney模拟API生图调用
    目前Midjourney没有对外开放API接口,所以通过MJ自动化生图的主要方式是,集成Discord应用机器人,通过机器人与MJ机器人进行交互,并监听频道内的生图结果,最终拿到图片地址。简单介绍下步骤一、购买MJ账号二、获取账号Authorization在网页中向MidjourneyBot发送/imagine进行生图我......
  • 网页在线编辑Excel表格,调用本机Office,非模拟,插入图片,导出PDF全屏编辑
    Excel作为微软Office的重要组成部分,在各行业中应用非常广泛,随着互联网及云计算的普及,网页在线编辑Excel表格也越来越成文更多人的广泛需求,而在网页中却无法直接在线编辑微软Office,下面介绍一种Web网页在线编辑Excel表格方案,可以调用本机原生Offce软件,直接在网页上编辑Excel表格。......
  • Python 调用 FFmpeg 处理合并视频文件
    ​ FFmpeg是一个开源的多媒体框架,它包含了用于处理音频、视频、字幕等多媒体数据的一系列工具、库和软件包。FFmpeg可以执行多种多媒体处理任务,包括转码、剪辑、合并、分离、编解码、流媒体传输等。它被广泛用于多媒体应用程序和流媒体平台中,是一个功能强大且高度可定制的工......
  • Python中强大的动态类型特性,以方法调用为例
    在研究大佬的项目时,从一行行代码溯源,拨茧抽丝的过程中,发现了方法调用的“神奇之处”具体情况如下:1.在类Trainer中名为run等方法中有加载预训练好的模型的load方法2.load()方法依旧是类方法中的一个,在load方法中有具体的load_self()方法3. load_self()定义在另一个py文件mod......
  • element ui中同级button调用upload组件
    代码<el-uploadref="schoolLogo"class="avatar-uploader"action="https://jsonplaceholder.typicode.com/posts/":show-file-list="false":on-success="handleAvatarSuccess":before-u......
  • 智慧安防视频监控可视化平台EasyCVR调用接口返回“Unauthorized”是什么原因?
    智慧安防视频监控可视化平台EasyCVR采用了开放式的网络结构,平台能在局域网、公网、专网等复杂的网络环境中,将场景中分散的海量网络监控设备进行统一接入与汇聚管理,并能提供实时远程视频监控、视频录像、录像回放与存储、告警、语音对讲、云台控制、平台级联、磁盘阵列存储、视频集......
  • 腾讯云api-python调用
    https://cloud.tencent.com/document/product/1278/46716#-*-coding:utf-8-*-importhashlib,hmac,json,os,sys,timefromdatetimeimportdatetime#密钥参数#需要设置环境变量TENCENTCLOUD_SECRET_ID,值为示例的AKIDz8krbsJ5yK**********mLPx3EXAMPLEsecret_......