Bosch
Bosch

Reputation: 51

Delphi - Write/read files in a shared environment

I have some problems on files which are placed in a LAN: there is a single Delphi program (server) which should write some files, which can be only read by multiple Deplhi programs (clients). I use these simple instructions in the server for writing (DataList is a TStrings):

Stream:=TFileStream.Create(filePath,fmOpenWrite or fmShareDenyWrite);
try
 DataList.SaveToStream(Stream);
finally
 Stream.Free;
end;

The clients check every 5 seconds if the file above is modified (by just checking the FileAge), and if modifications are occurred, they load the DataList in the following way:

try
 Stream:=TFileStream.Create(filePath,fmOpenRead or fmShareDenyNone);
 DataList.LoadFromStream(Stream);
finally
 Stream.Free;
end;

Normally everything works perfectly, but sometimes it happens that the server or the client raise an exception because "the file is in use by other process". I don't understand which is the problem: I tried many alternatives, but this can happen also with just the server and only one istance of the client running..

Any ideas?

Thanks!

Upvotes: 5

Views: 3425

Answers (1)

Arnaud Bouchez
Arnaud Bouchez

Reputation: 43043

By design, network file systems can't be trusted. At least, NFS (in Linux) and SMB (in Windows) have no proven lock feature: concurrent access is not safe.

You need to use a Client-Server protocol to ensure that shared data is safe. You can use TCP/IP, HTTP or any other mean.

I recommend using a true service implementation, like DataSnap, RemObjects or our Open Source mORMot.

Upvotes: 3

Related Questions