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