The Server Application

The server side of the distributed application informs the system that its services are available and then waits for client requests.

Depending on the size of your application and your coding preferences, you can choose to implement remote procedures in one or more separate files. In our example, the main server routine is in the source file hellos.c, and the remote procedure remains in the file hellop.c, which we created for the stand-alone program.

The benefit of organizing the remote procedures in separate files is that the procedures can be linked with a stand-alone program to debug the code before it is converted to a distributed application. After the program works as a stand-alone program, you can compile and link the remote-procedure source files with the server application.

As with the client-application source file, the server-application source file must include the hello.h header file to obtain definitions for the RPC data and functions and for the interface-specific data and functions.

The server calls the RPC runtime functions RpcServerUseProtseqEp and RpcServerRegisterIf to make binding information available to the client. Since we have only one implentation of our remote procedures, we only pass the interface handle name to RpcServerRegisterIf. The other parameters are set to NULL. The server then calls the RpcServerListen function to indicate that it is waiting for client requests.

The server application must also include the two memory management functions that the server stub calls — midl_user_allocate and midl_user_free. These functions allocate and free memory on the server when a remote procedure passes parameters to the server. In our example, midl_user_allocate and midl_user_free are simply wrappers for the C-library functions malloc and free. (Note that, in the MIDL compiler- generated forward declarations, "midl" is uppercase. The header file rpcndr.h defines midl_user_free and midl_user_allocate to be MIDL_user_free and MIDL_user_allocate, respectively.)

/* file: hellos.c */
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
#include "hello.h"
 
void main()
{
    RPC_STATUS status;
    unsigned char * pszProtocolSequence = "ncacn_np";
    unsigned char * pszSecurity     = NULL; /*Security not implemented */
    unsigned char * pszEndpoint    = "\\pipe\\hello";
    unsigned int    cMinCalls           = 1;
    unsigned int    cMaxCalls           = 20;
    unsigned int    fDontWait           = FALSE;
 
    status = RpcServerUseProtseqEp(pszProtocolSequence,
                                   cMaxCalls,
                                   pszEndpoint,
                                   pszSecurity); 
 
    if (status) {
        exit(status);
    }
 
    status = RpcServerRegisterIf(hello_v1_0_s_ifspec,  
                                 NULL,   
                                 NULL); 
 
    if (status) {
        exit(status);
    }
 
    status = RpcServerListen(cMinCalls,
                             cMaxCalls,
                             fDontWait);
 
    if (status) {
        exit(status);
    }
 
 }  // end main()
 
/******************************************************/
/*         MIDL allocate and free                     */
/******************************************************/
 
void __RPC_FAR * __RPC_USER midl_user_allocate(size_t len)
{
    return(malloc(len));
}
 
void __RPC_USER midl_user_free(void __RPC_FAR * ptr)
{
    free(ptr);
}