From: keeper@mindspring.com (Mark Johnson)
Subject: Re: network
Date: Sun, 18 Jun 1995 17:49:45 -0500

>Hi,
>I have written a program in Pascal and I would like it to be
>used on a network, where over 30 people with use it at the same
>time. The only time the data file is accessed is when they have
>finished working and it saves, or they ask for info from the data
>file.
>
>How do I make it compatible and is it hard?
>
>Thanking you
>Shaun Swartz

In order to function well on a network environment in which users may
be simultaneously reading and writing to a file, you would want to make
use of the Share file locking/sharing functionality.  (SHARE.EXE is
a DOS program that is typically required by Windows.  There are also
other programs that perform the same functions as SHARE.)

Unfortunately, Delphi (and other Borland Pascal compilers) does not
provide a great deal of support for the shared file functionality.
What is provided for in Delphi is very poorly documented.  Nonetheless,
there are two ways to make it work:

1. Use a TFileStream class to handle all file access.  (TFileStream
   supports Share functionality via the Mode parameter to its Create()
   method.)

2. Use the FileOpen(), FileRead(), FileWrite, and FileClose() functions
   for all file access.  (In fact, these are the functions that
   TFileStream uses.)

I would recommend that you use the TFileStream object since it will
provide you with greater control and flexibility for reading and writing
data.  It also has a little more documentation than the FileXXXX()
functions (though still not much).

Basically, you would probably want to create a TFileStream object
giving "fmOpenRead + fmShareDenyWrite" when reading data from your
shared file, and "fmOpenWrite + fmShareExclusive" when writing data
to the shared file.  This will protect the file during read and
write operations in order to avoid conflicts and potential
corruption of the data.

In addition to using these values to protect the file, you will also
need to put some sort of "try again" mechanism in your code.  For
example, if one program has openned the file for reading and another
attempts to open the file for writing, the attempt will fail.  Your
program has to expect that sometimes the file will already be openned
by someone else.  It should deal with the problem by either pausing
for a short period of time and trying again, or telling the user that
the file is blocked and that the attempt should be tried again at a
later time.

A final tip:  Try to keep your file reading & writing operations as
small and quick as possible.  As long as one of the programs has the
file open, it's blocking someone else from using it...

I hope this helps.  If there's anything else I can do, just
let me know.

--Mark Johnson
______________________________________________________________________________
 Mark R. Johnson       _/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_
 Atlanta, GA USA       _/  http://www.mindspring.com/~cityzoo/cityzoo.html  _/
 keeper@mindspring.com _/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/


