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