PreviousNext

The greet Application: An Implementation Using DCE DFS

This section describes an implementation of the greet application using the DCE Distributed File Service. In this version, the client and server use well-known files in the DCE filespace to communicate with each other.

This application looks just like an application using a local file system, except for the names of the files in the DCE filespace. The communication (using RPC) is done by DFS, and is not visible to the programmer.

(Please note that this example is intended to be simple, not necessarily to model good programming. For example, a real application would check return values for errors, and would be likely to use the lock system call to synchronize client and server access to files, rather than waking up every few seconds to check if a file had been created.)

The application contains three files: dfs_greet.h, dfs_greet_client.c, and dfs_greet_server.c.

· The dfs_greet.h File

This file gives the well-known filenames that the client and server communicate through.

/*
* DCE Program Example Using DFS
*
* dfs_greet.h
*/

#define C_GREET_FILE "/.../my_cell/fs/opt/my_company/greet/client"
#define S_GREET_FILE "/.../my_cell/fs/opt/my_company/greet/server"

· The dfs_greet_client.c File

This is the client side of the application.

/*
* DCE Program Example Using DFS
* dfs_greet_client.c
*
* The client writes a message for the server into
* a well-known file. It waits until the server has
* created its own well-known file, then reads the
* server's message from the file, prints it, and
* deletes the file.
*/

#include <stdio.h>
#include "dfs_greet.h"

#define C_GREET_TEXT "Hi, server!"

main()
{
FILE *f;
size_t ret;
char s[BUFSIZ];

f = fopen(C_GREET_FILE, "w");
ret = fwrite(C_GREET_TEXT, sizeof(C_GREET_TEXT), 1, f);
fclose(f);
while ((f = fopen(S_GREET_FILE, "r")) == NULL)
sleep(3);
ret = fread(s, sizeof(char), BUFSIZ, f);
fclose(f);
printf("Server says: %s\n", s);
unlink(S_GREET_FILE);
}

· The dfs_greet_server.c File

This file contains the server side of the greet application.

/*
* DCE Example Program Using DFS
* dfs_greet_server.c
*
* The server waits until the client has created a
* well-known file, then reads the client's message
* from the file, prints the message, and removed the
* file. The server then writes a message for the
* client into another well-known file.
*/

#include <stdio.h>
#include "dfs_greet.h"

#define S_GREET_TEXT "Hi, client!"

main()
{
FILE *f;
size_t ret;
char s[BUFSIZ];

while ((f = fopen(C_GREET_FILE, "r")) == NULL)
sleep(3);
ret = fread(s, sizeof(char), BUFSIZ, f);
fclose(f);
printf("Client says: %s\n", s);
unlink(C_GREET_FILE);

f = fopen(S_GREET_FILE, "w");
ret = fwrite(S_GREET_TEXT, sizeof(S_GREET_TEXT), 1, f);
fclose(f);
}

The Makefile for creating the client and server programs is as follows:

# Makefile for DCE Program Example Using DFS


all: dfs_greet_client dfs_greet_server

dfs_greet_client: dfs_greet.h dfs_greet_client.c
cc -o dfs_greet_client dfs_greet_client.c

dfs_greet_server: dfs_greet.h dfs_greet_server.c
cc -o dfs_greet_server dfs_greet_server.c

The greet client and greet server are installed as in the RPC application. They are run in the same way, except they do not take a servername argument.