xref: /freebsd/share/examples/sunrpc/msg/rprintmsg.c (revision 944fcc15f83de276d019fb56fc70dc7312d0695e)
1944fcc15SGarrett Wollman /* @(#)rprintmsg.c	2.1 88/08/11 4.0 RPCSRC */
2944fcc15SGarrett Wollman /*
3944fcc15SGarrett Wollman  * rprintmsg.c: remote version of "printmsg.c"
4944fcc15SGarrett Wollman  */
5944fcc15SGarrett Wollman #include <stdio.h>
6944fcc15SGarrett Wollman #include <rpc/rpc.h>		/* always need this */
7944fcc15SGarrett Wollman #include "msg.h"		/* need this too: will be generated by rpcgen*/
8944fcc15SGarrett Wollman 
9944fcc15SGarrett Wollman main(argc, argv)
10944fcc15SGarrett Wollman 	int argc;
11944fcc15SGarrett Wollman 	char *argv[];
12944fcc15SGarrett Wollman {
13944fcc15SGarrett Wollman 	CLIENT *cl;
14944fcc15SGarrett Wollman 	int *result;
15944fcc15SGarrett Wollman 	char *server;
16944fcc15SGarrett Wollman 	char *message;
17944fcc15SGarrett Wollman 
18944fcc15SGarrett Wollman 	if (argc < 3) {
19944fcc15SGarrett Wollman 		fprintf(stderr, "usage: %s host message\n", argv[0]);
20944fcc15SGarrett Wollman 		exit(1);
21944fcc15SGarrett Wollman 	}
22944fcc15SGarrett Wollman 
23944fcc15SGarrett Wollman 	/*
24944fcc15SGarrett Wollman 	 * Remember what our command line arguments refer to
25944fcc15SGarrett Wollman 	 */
26944fcc15SGarrett Wollman 	server = argv[1];
27944fcc15SGarrett Wollman 	message = argv[2];
28944fcc15SGarrett Wollman 
29944fcc15SGarrett Wollman 	/*
30944fcc15SGarrett Wollman 	 * Create client "handle" used for calling MESSAGEPROG on the
31944fcc15SGarrett Wollman 	 * server designated on the command line. We tell the rpc package
32944fcc15SGarrett Wollman 	 * to use the "tcp" protocol when contacting the server.
33944fcc15SGarrett Wollman 	 */
34944fcc15SGarrett Wollman 	cl = clnt_create(server, MESSAGEPROG, MESSAGEVERS, "tcp");
35944fcc15SGarrett Wollman 	if (cl == NULL) {
36944fcc15SGarrett Wollman 		/*
37944fcc15SGarrett Wollman 		 * Couldn't establish connection with server.
38944fcc15SGarrett Wollman 		 * Print error message and die.
39944fcc15SGarrett Wollman 		 */
40944fcc15SGarrett Wollman 		clnt_pcreateerror(server);
41944fcc15SGarrett Wollman 		exit(1);
42944fcc15SGarrett Wollman 	}
43944fcc15SGarrett Wollman 
44944fcc15SGarrett Wollman 	/*
45944fcc15SGarrett Wollman 	 * Call the remote procedure "printmessage" on the server
46944fcc15SGarrett Wollman 	 */
47944fcc15SGarrett Wollman 	result = printmessage_1(&message, cl);
48944fcc15SGarrett Wollman 	if (result == NULL) {
49944fcc15SGarrett Wollman 		/*
50944fcc15SGarrett Wollman 		 * An error occurred while calling the server.
51944fcc15SGarrett Wollman 	 	 * Print error message and die.
52944fcc15SGarrett Wollman 		 */
53944fcc15SGarrett Wollman 		clnt_perror(cl, server);
54944fcc15SGarrett Wollman 		exit(1);
55944fcc15SGarrett Wollman 	}
56944fcc15SGarrett Wollman 
57944fcc15SGarrett Wollman 	/*
58944fcc15SGarrett Wollman 	 * Okay, we successfully called the remote procedure.
59944fcc15SGarrett Wollman 	 */
60944fcc15SGarrett Wollman 	if (*result == 0) {
61944fcc15SGarrett Wollman 		/*
62944fcc15SGarrett Wollman 		 * Server was unable to print our message.
63944fcc15SGarrett Wollman 		 * Print error message and die.
64944fcc15SGarrett Wollman 		 */
65944fcc15SGarrett Wollman 		fprintf(stderr, "%s: sorry, %s couldn't print your message\n",
66944fcc15SGarrett Wollman 			argv[0], server);
67944fcc15SGarrett Wollman 		exit(1);
68944fcc15SGarrett Wollman 	}
69944fcc15SGarrett Wollman 
70944fcc15SGarrett Wollman 	/*
71944fcc15SGarrett Wollman 	 * The message got printed on the server's console
72944fcc15SGarrett Wollman 	 */
73944fcc15SGarrett Wollman 	printf("Message delivered to %s!\n", server);
74944fcc15SGarrett Wollman }
75