ONC+ 開発ガイド

メッセージ表示プログラムとその遠隔バージョン


例 D-7 printmesg.c

/* printmsg.c: コンソールにメッセージを表示 */
#include <stdio.h>

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

	if (argc != 2) {
		fprintf(stderr, "usage: %s <message>¥n", argv[0]);
		exit(1);
	}
	message = argv[1];
	if( !printmessage(message) ) {
		fprintf(stderr, "%s: couldn't print your message¥n",
		        argv[0]);
		exit(1);
	}
	printf("Message Delivered!¥n");
	exit(0);
}

/* コンソールにメッセージを表示します。 */

/*
 * メッセージが実際に表示されたかどうかを示すブール値を返します。
 */
printmessage(msg)
	char *msg;
{
	FILE *f;

	if = fopen("/dev/console","w");
		if (f == (FILE *)NULL)
			return (0);
	fprintf(f,"%sen", msg);
	fclose(f);
	return (1);
}


例 D-8 printmesg.c の遠隔バージョン

/*  * rprintmsg.c: printmsg.c の遠隔バージョン  */ 
#include <stdio.h> 
#include <rpc/rpc.h> /* 必ず必要 */ 
#include "msg.h"     /* msg.h は rpcgen が生成 */ 

main(argc, argv) 
	int argc;
	char *argv[];
{
	CLIENT *cl; 
	int *result;  	
	char *server;
	char *message;
	extern int sys_nerr;
	extern char *sys_errlist[]; 

	if (argc != 3) { 
		fprintf(stderr,"usage: %s host messagen", argv[0]);
		exit(1);
	}
	/* 
	 * コマンド行で指定した引数の値を保存します。
	 */
	server = argv[1];
	message = argv[2]; 
/*
 * コマンド行で指定したサーバ上の MESSAGEPROG の呼び出しに使用する
 * クライアント「ハンドル」を作成します。
 */ 
	cl = clnt_create(server, MESSAGEPROG, PRINTMESSAGEVERS,
	                 "visible");
	if (cl == (CLIENT *)NULL) {
		/* 
		 * サーバとの接続の確立に失敗。
		 * エラーメッセージを表示して終了します。
		 */
		clnt_pcreateerror(server);
		exit(1);
	}
	/* サーバ上の遠隔手続き printmessage を呼び出します。r */
	result = printmessage_1(&message, cl);
	if (result == (int *)NULL) {  	
	/*
 * サーバの呼び出しでエラーが発生。
	 * エラーメッセージを表示して終了します。
	 */ 
		clnt_perror(cl, server);
		exit(1);
	}
	/* 遠隔手続きの呼び出しに成功。 */
	if (*result == 0) { 
		/*
		 * サーバはメッセージの表示に失敗。
		 * エラーメッセージを表示して終了します。
		 */
		fprintf(stderr,"%s"
	}
/* サーバのコンソールにメッセージが出力されました。 */
	printf("Message delivered to %s!¥n", server);
	exit(0);
}


例 D-9 rpcgen プログラム : msg.x

/* msg.x: 遠隔メッセージ印刷プロトコル */
program MESSAGEPROG {
 	version MESSAGEVERS {
 		int PRINTMESSAGE(string) = 1;
 	} = 1;
} = 0x20000001;


例 D-10 mesg_proc.c

/*
 *  msg_proc.c: 遠隔手続き printmessage
 */

#include <stdio.h>
#include <rpc/rpc.h> 	/* 必ず必要 */
#include "msg.h" 	/* msg.h は rpcgen が生成 */

/*
 * printmessage の遠隔バージョン
 */
/* 使用する引数 */
int printmessage_1(msg, req)
	char **msg;
	struct svc_req *req;
{
	static int result; /* 必ず static で宣言 */
	FILE *f;

	f = fopen("/dev/console", "w");
	if (f == (FILE *)NULL) {
		result = 0;
		return (&result);
	}
	fprintf(f, "%sen", *msg);
 	fclose(f);
 	result = 1;
 	return (&result);
}