PreviousNext

Writing the Client Code

This topic describes the client program for the greet application, whose interface definition was shown earlier in this topic.

The client performs the following major steps:

1. It checks the command-line arguments for an entry name to use for its search in the namespace.

2. It calls rpc_ns_binding_import_begin( ) to start the search in the namespace.

3. It calls rpc_ns_binding_import_next( ) to obtain a binding to a server.

4. It calls the greet remote procedure with a string greeting.

5. It prints the reply from the server.

The greet_client.c module is as follows:

/*

* greet_client.c

*

* Client of "greet" interface.

*/

#include <stdio.h>

#include <dce/rpc.h>

#include "greet.h"

#include "util.h"

int

main(

int argc,

char *argv[]

)

{

rpc_ns_handle_t import_context;

handle_t binding_h;

error_status_t status;

idl_char reply[REPLY_SIZE];

if (argc < 2) {

fprintf(stderr, "usage: greet_client <CDS pathname>\n");

exit(1);

}

/*

* Start importing servers using the name specified

* on the command line.

*/

rpc_ns_binding_import_begin(

rpc_c_ns_syntax_default, (unsigned_char_p_t) argv[1],

greetif_v1_0_c_ifspec, NULL, &import_context, &status);

ERROR_CHECK(status, "Can't begin import");

/*

* Import the first server (we could iterate here,

* but we'll just take the first one).

*/

rpc_ns_binding_import_next(import_context, &binding_h, &status);

ERROR_CHECK(status, "Can't import");

/*

* Make the remote call.

*/

greet(binding_h, (idl_char *) "hello, server", reply);

printf("The Greet Server said: %s\n", reply);

}

The module first includes greet.h, the header file for the greet interface generated by the IDL compiler.

In this example, after each call to an RPC runtime routine, the client program calls the application-specific ERROR_CHECK macro. If the status from the RPC runtime routine is not error_status_ok, dce_error_inq_text( ) is called and the error message is printed.

As specified in the greet.idl interface definition, the greet application uses explicit handles. The client therefore passes a binding handle of type handle_t as the first parameter of the greet procedure. At runtime, when the client makes its first remote procedure call, the handle is only partially bound because the client does not know the particular endpoint on which the server is listening; for delivery of its requests to the server endpoint, the client depends on the endpoint mapping service of the dced process on the server host.