Einfache Funktion (msg_local.c)

Einfache Funktion (msg_local.c)
#include <stdio.h>
int helloworld (char * wetter) {
printf ("hello world\n");
printf ("%s\n", wetter);
return (1);
}
Einfaches main (hello.c)
#include <stdio.h>
#include "msg_local.c"
int main (int argc, char * argv[]) {
char * message;
if (argc != 2) {
fprintf (stdout, "usage: %s <message>\n", argv[0]);
exit(1);
}
message = argv[1];
if (!helloworld(message)) {
fprintf (stdout, "%S: couldn't print your message\n", argv[0]);
exit(1);
}
printf ("Message delivered\n");
exit (0);
}
Remote Variante der Funktion (msg_proc.c)
#include <stdio.h>
#include <rpc.h>
#include "msg.h"
/* Remote Version */
int *
helloworld_1 (char ** wetter) {
static int result;
printf ("hello world\n"); // Problem ?
printf ("%s\n", wetter);
result = 1;
return (&result);
}
Remote Aufruf in main (1) (rhello.c)
#include <stdio.h>
#include <rpc.h>
#include "msg_proc.c"
int main (int argc, char * argv[]) {
CLIENT *cl;
int * result;
char * server;
char * message;
if (argc != 3) {
printf ("usage: %s <host> <message>\n", argv[0]);
exit(1);
}
server = argv[1];
message = argv[2];
Remote Aufruf in main (2)
cl = clnt_create (server, MESSAGEPROG, MESSAGEVERS, "tcp");
if (cl == NULL) { clnt_pcreateerror (server);
exit (1);
}
result = helloworld_1(&message, cl);
if (result == NULL) {
clnt_perror (cl, server);
exit(1);
}
if (*result == 0) {
printf ("%s: %s couldn't print your message", argv[0], server);
exit (1);
}
printf ("Message delivered to %s\n", server);
exit (0);
}
Vorbereitung RPC
// file: msg.x
program MESSAGEPROG {
version MESSAGEVERS {
int HELLOWORLD(string) =1;
} = 1;
} = 99;
­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­
> rpcgen msg.x
=> neue Dateien: msg.h, msg_svc.c, msg_clnt.c
msg.h (1)
/*
* Please do not edit this file.
* It was generated using rpcgen.
*/
#ifndef _MSG_H_RPCGEN
#define _MSG_H_RPCGEN
#include <rpc/rpc.h>
#ifdef __cplusplus
extern "C" {
#endif
msg.h (2)
#define MESSAGEPROG 99
#define MESSAGEVERS 1
#if defined(__STDC__) || defined(__cplusplus)
#define HELLOWORLD 1
extern int * helloworld_1(char **, CLIENT *);
extern int * helloworld_1_svc(char **, struct svc_req *);
extern int messageprog_1_freeresult (SVCXPRT *, xdrproc_t, caddr_t);
#else /* K&R C */
#define HELLOWORLD 1
extern int * helloworld_1();
extern int * helloworld_1_svc();
extern int messageprog_1_freeresult ();
#endif /* K&R C */
#ifdef __cplusplus
}
#endif
#endif /* !_MSG_H_RPCGEN */