xref: /freebsd/share/examples/sunrpc/msg/rprintmsg.c (revision dd41de95a84d979615a2ef11df6850622bf6184e)
1 /* @(#)rprintmsg.c	2.1 88/08/11 4.0 RPCSRC */
2 /*
3  * rprintmsg.c: remote version of "printmsg.c"
4  */
5 #include <stdio.h>
6 #include <rpc/rpc.h>		/* always need this */
7 #include "msg.h"		/* need this too: will be generated by rpcgen*/
8 
9 main(argc, argv)
10 	int argc;
11 	char *argv[];
12 {
13 	CLIENT *cl;
14 	int *result;
15 	char *server;
16 	char *message;
17 
18 	if (argc < 3) {
19 		fprintf(stderr, "usage: %s host message\n", argv[0]);
20 		exit(1);
21 	}
22 
23 	/*
24 	 * Remember what our command line arguments refer to
25 	 */
26 	server = argv[1];
27 	message = argv[2];
28 
29 	/*
30 	 * Create client "handle" used for calling MESSAGEPROG on the
31 	 * server designated on the command line. We tell the rpc package
32 	 * to use the "tcp" protocol when contacting the server.
33 	 */
34 	cl = clnt_create(server, MESSAGEPROG, MESSAGEVERS, "tcp");
35 	if (cl == NULL) {
36 		/*
37 		 * Couldn't establish connection with server.
38 		 * Print error message and die.
39 		 */
40 		clnt_pcreateerror(server);
41 		exit(1);
42 	}
43 
44 	/*
45 	 * Call the remote procedure "printmessage" on the server
46 	 */
47 	result = printmessage_1(&message, cl);
48 	if (result == NULL) {
49 		/*
50 		 * An error occurred while calling the server.
51 	 	 * Print error message and die.
52 		 */
53 		clnt_perror(cl, server);
54 		exit(1);
55 	}
56 
57 	/*
58 	 * Okay, we successfully called the remote procedure.
59 	 */
60 	if (*result == 0) {
61 		/*
62 		 * Server was unable to print our message.
63 		 * Print error message and die.
64 		 */
65 		fprintf(stderr, "%s: sorry, %s couldn't print your message\n",
66 			argv[0], server);
67 		exit(1);
68 	}
69 
70 	/*
71 	 * The message got printed on the server's console
72 	 */
73 	printf("Message delivered to %s!\n", server);
74 }
75