Archive note: Originally published on Tune2Wizard on 3 April 2014. Restored and reformatted on 4 December 2026. The original hand-drawn illustration is preserved. The historical example is retained with small correctness and clarity improvements; it has not been rebuilt on a modern Linux distribution.

Introduction

Remote Procedure Call (RPC) allows a client to invoke a procedure in a server process across a network. The client and server have separate address spaces and can run on different machines or processor architectures.

In this article, I explore Sun ONC RPC (Open Network Computing RPC) using C on Linux. One application is communication between two embedded systems connected through Ethernet, where one SoC runs Linux and another runs a different operating system.

Client-server relationship

The client validates input and dispatches a request. The server implements the core business logic and returns the result. RPC hides much of the networking machinery behind a procedure-like interface, although remote calls can still fail or time out.

RPC — underlying concept

The mechanism is similar to the marshalling and unmarshalling used in D-Bus. ONC RPC uses External Data Representation (XDR) to encode values in a machine-independent binary format. RPC messages can travel over TCP or UDP. The rpcbind/portmapper service, traditionally reached on port 111, helps clients discover the registered RPC service; it does not forward every RPC payload.

Original hand-drawn ONC RPC diagram showing client and server processes, procedure calls, stubs and network routines
Original Tune2Wizard illustration (2014): client and server stubs bridge separate process address spaces through network routines.

The client stub marshals arguments, invokes the remote service through the network routines and unmarshals the result. The server stub unmarshals incoming arguments, invokes the local procedure and marshals the result for transmission back to the client.

Interface definition and code generation

ONC RPC uses an interface definition file with the .x extension. It defines data types, procedures, versions and program identifiers. The traditional interface takes one argument and returns one result; structures can group multiple values.

The rpcgen utility generates C stubs and supporting files, allowing the developer to focus on application logic.

rpcgen -a -C temp.x
Generated file Responsibility
temp.h Shared types and procedure declarations
temp_clnt.c Client stub
temp_svc.c Server stub and service registration
temp_client.c Client application template
temp_server.c Server procedure template
temp_xdr.c XDR routines when needed for custom types
Makefile.temp Build instructions

To generate individual templates:

rpcgen -Sc -C temp.x > temp_client.c
rpcgen -Ss -C temp.x > temp_server.c
rpcgen -Sm -C temp.x > Makefile.temp

Practical example — a temperature conversion service

The goal is to run a server that converts Celsius to Kelvin and a client that repeatedly invokes the remote conversion procedure. The original exercise uses -999 as a sentinel to terminate the demonstration.

Step 1 — Define the interface

Create temp.x:

program TEMPSERV {
    version TEMPVERS {
        int TempConv(int) = 1;
    } = 1;
} = 77;

Each procedure is identified by the program number, version number and procedure number. The number 77 is retained from the historical example, not recommended as a globally unique production identifier.

Generate the template files:

rpcgen -a -C temp.x

Step 2 — Implement the server

Edit temp_server.c:

#include "temp.h"
#include <stdio.h>
#include <stdlib.h>

int *
tempconv_1_svc(int *argp, struct svc_req *rqstp)
{
    static int result;
    (void)rqstp;

    if (*argp == -999) {
        printf("Server function exit\n");
        exit(0);
    }

    result = *argp + 273;
    printf("The temperature in Kelvin is approximately %d\n", result);
    return &result;
}

Restoration correction: The original article used an offset of 274 (and mentioned 275 in prose). The correct conversion is K = °C + 273.15; the integer example above uses 273 as a rounded offset to retain the original interface. The static result follows the simple generated-template style, not a general thread-safe design.

Step 3 — Implement the client

Edit temp_client.c:

#include "temp.h"
#include <stdio.h>
#include <stdlib.h>

static void tempserv_1(char *host)
{
    CLIENT *clnt = clnt_create(host, TEMPSERV, TEMPVERS, "udp");
    if (clnt == NULL) {
        clnt_pcreateerror(host);
        exit(EXIT_FAILURE);
    }

    for (;;) {
        int celsius;
        printf("Enter the temperature in Celsius: ");
        if (scanf("%d", &celsius) != 1)
            break;

        int *result = tempconv_1(&celsius, clnt);
        if (celsius == -999)
            break;
        if (result == NULL) {
            clnt_perror(clnt, "call failed");
            break;
        }
        printf("The temperature in Kelvin is approximately %d\n", *result);
    }

    clnt_destroy(clnt);
}

int main(int argc, char **argv)
{
    if (argc != 2) {
        fprintf(stderr, "Usage: %s server_host\n", argv[0]);
        return EXIT_FAILURE;
    }
    tempserv_1(argv[1]);
    return EXIT_SUCCESS;
}

The transport can be selected in clnt_create: the historical example uses udp; tcp is another option when the server supports it.

The sentinel deliberately terminates the server before a normal RPC response is returned, so that last call may time out or fail. This is a demonstration-only shutdown mechanism, not a production API design.

Step 4 — Compile and run

The original tutorial used the generated makefile with debug flags and foreground execution:

CFLAGS += -g -DRPC_SVC_FG
RPCGENFLAGS = -C
make -f Makefile.temp

On the server machine:

./temp_server

On the client machine (the historical example used 10.0.0.10):

./temp_client 10.0.0.10

Enter a Celsius temperature to invoke the remote function. Entering -999 terminates the demonstration. Modern Linux installations may require rpcgen, rpcbind, and TI-RPC development headers and libraries, along with compatible build flags. Run this example only in an isolated, trusted lab; the demonstration does not implement authentication or encryption.

Revisiting RPC in 2026 — the connection to gRPC

While studying system design, I revisited this example and recognised that many of the underlying engineering concepts still apply to modern service-to-service communication.

ONC RPC gRPC (typical configuration)
.x interface definition .proto service and message definition
rpcgen protoc and gRPC code generation plugin
XDR binary representation Protocol Buffers binary encoding
Client and server stubs Generated client and server interfaces
TCP or UDP HTTP/2 over TCP

The technologies are different, but the pattern is familiar: define a contract, generate communication code, serialise arguments, invoke the remote procedure and deserialise the result.

In modern architecture, I would evaluate gRPC for performance-sensitive internal services where both sides can support the contract and tooling. REST remains a useful default for public-facing integrations where broad interoperability matters. The decision should follow requirements rather than protocol popularity alone.

Conclusion

RPC illustrates a fundamental idea in distributed systems: the caller and the implementation can reside in separate processes or machines while communicating through an agreed interface. Code generation and machine-independent serialisation simplify that interaction, but network failure, compatibility and operational concerns remain part of the design.

References