2.5. Example Code

There are several example programs included in the srr-x.x.x/exe/ directory. At the time of this writing, the following programs are included, both as source code and compiled:

Shown below is the contents of file srr-x.x.x/exe/send.c:

[Note]

This code is shown as an example only, and may be slightly out of date for your version of the SRR Module. Please use the sample programs in your distribution to ensure full compatibility.

/*
 * send.c: a simple program that sends a message, and prints the reply
 *
 * $Id: sr_man.sgml,v 1.40 2010-01-13 20:26:26 robert Exp $
 */

#include <assert.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include "srr.h"

int main(int argc, char* argv[])
{
        int             opt, len;
        int             pid = 0;
        char*   name = 0;
        char    smsg[BUFSIZ] = "";
        char    rmsg[BUFSIZ] = "";

        sprintf(smsg, "msg from %d", getpid());

        while((opt = getopt(argc, argv, "p:n:")) != -1)
        {
                switch(opt)
                {
                case 'p':
                        pid = atoi(optarg);
                        break;

                case 'n':
                        name = optarg;
                        break;

                default:
                        exit(1);
                }
        }

        if(SrrReg() == -1)
        {
                printf("send %d: InitSrr() failed: [%d] %s\n",
                        getpid(), errno, strerror(errno));

                exit(1);
        }

        if(name)
        {
                if((pid = qnx_name_locate(0, name, 0, 0)) == -1)
                {
                        printf("send %d: qnx_name_locate() failed: [%d] %s\n",
                                getpid(), errno, strerror(errno));
                        exit(1);
                }
        }

        printf("send %d: sending: \"%s\"\n", getpid(), smsg);

        if((len = SrrSend(pid, smsg, rmsg, strlen(smsg)+1, BUFSIZ)) == -1)
        {
                printf("send %d: Send() failed: [%d] %s\n",
                        getpid(), errno, strerror(errno));
                exit(1);
        }

        printf("send %d: received len %d: \"%s\"\n", getpid(), len, rmsg);

        return 0;
}