Path: blob/main/share/examples/sunrpc/msg/rprintmsg.c
39530 views
/*1* rprintmsg.c: remote version of "printmsg.c"2*/3#include <stdio.h>4#include <rpc/rpc.h> /* always need this */5#include "msg.h" /* need this too: will be generated by rpcgen*/67main(argc, argv)8int argc;9char *argv[];10{11CLIENT *cl;12int *result;13char *server;14char *message;1516if (argc < 3) {17fprintf(stderr, "usage: %s host message\n", argv[0]);18exit(1);19}2021/*22* Remember what our command line arguments refer to23*/24server = argv[1];25message = argv[2];2627/*28* Create client "handle" used for calling MESSAGEPROG on the29* server designated on the command line. We tell the rpc package30* to use the "tcp" protocol when contacting the server.31*/32cl = clnt_create(server, MESSAGEPROG, MESSAGEVERS, "tcp");33if (cl == NULL) {34/*35* Couldn't establish connection with server.36* Print error message and die.37*/38clnt_pcreateerror(server);39exit(1);40}4142/*43* Call the remote procedure "printmessage" on the server44*/45result = printmessage_1(&message, cl);46if (result == NULL) {47/*48* An error occurred while calling the server.49* Print error message and die.50*/51clnt_perror(cl, server);52exit(1);53}5455/*56* Okay, we successfully called the remote procedure.57*/58if (*result == 0) {59/*60* Server was unable to print our message.61* Print error message and die.62*/63fprintf(stderr, "%s: sorry, %s couldn't print your message\n",64argv[0], server);65exit(1);66}6768/*69* The message got printed on the server's console70*/71printf("Message delivered to %s!\n", server);72}737475