CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    May 2012
    Posts
    6

    Question Multiple apps accesing same file on a server

    Hey,
    I need to implement an application that will be installed on multiple PCs. These apps need to read/write a single file on a server (which is not mine). The problem I am facing is simultaneous access to this file. The file is not necessarily a file, it is a resource, an int value, which each app will increment an bring to a maximum after which the first app to find it over the maximum value will reset it to 0. Any ideas as to how to store this value on the server and how to access it will be very helpful. Thanks in advance.

    P.S. Because the server isn't mine (it's paid-for web hosting) I don't think server side code is an option. I also already have all the apps reading files from the server but each app has it's own http location so no simultaneous access happening there.

  2. #2
    Join Date
    Oct 2005
    Location
    Minnesota, U.S.A.
    Posts
    680

    Re: Multiple apps accesing same file on a server

    Look at LockFile() if is a shared file.

    If you are talking about something on a web server that you are accessing via HTTP GET/PUT, synchronization is very difficult. Normally a separate lock file is also needed. Wait for a 0 in the lock file, write a 1 to lock the main file, get the main file value, update it, then write a 0 to the lock file so other apps can use it.

    Note that you can also use a lock byte in the main file, but that is dangerous.

    -Erik

  3. #3
    Join Date
    May 2012
    Posts
    6

    Re: Multiple apps accesing same file on a server

    Quote Originally Posted by egawtry View Post
    Look at LockFile() if is a shared file.

    If you are talking about something on a web server that you are accessing via HTTP GET/PUT, synchronization is very difficult. Normally a separate lock file is also needed. Wait for a 0 in the lock file, write a 1 to lock the main file, get the main file value, update it, then write a 0 to the lock file so other apps can use it.

    Note that you can also use a lock byte in the main file, but that is dangerous.

    -Erik
    I'm not going to access via http get/put because I can't write to files via http. I am going to use ftp. I'll give lockfile a try. Although it doesn't seem to fix the "machines accessing the same file at the same time problem". What if 2 machines try to access the lockfile at the same time...or is that improbable?

  4. #4
    Join Date
    Oct 2005
    Location
    Minnesota, U.S.A.
    Posts
    680

    Re: Multiple apps accesing same file on a server

    Quote Originally Posted by bloodfont View Post
    I'm not going to access via http get/put because I can't write to files via http. I am going to use ftp.
    FTP is not a very good file sharing solution. And you will have to use the scheme I outlined for HTTP with it.

    Quote Originally Posted by bloodfont View Post
    What if 2 machines try to access the lockfile at the same time...or is that improbable?
    If you use a second file as a lock file, that can still mess up, but is the best you can do.




    LockFile() is only available if you have the file open on a mounted filesystem. FTP and HTTP do not fit that bill. Without server side access, you will not be able to do true file sharing.

    -Erik

  5. #5
    Join Date
    May 2012
    Posts
    6

    Re: Multiple apps accesing same file on a server

    Quote Originally Posted by egawtry View Post
    FTP is not a very good file sharing solution. And you will have to use the scheme I outlined for HTTP with it.



    If you use a second file as a lock file, that can still mess up, but is the best you can do.




    LockFile() is only available if you have the file open on a mounted filesystem. FTP and HTTP do not fit that bill. Without server side access, you will not be able to do true file sharing.

    -Erik
    I was thinking of designating a PC (not a server) to receive data from all the other apps, do the calculations, and respond to all the apps. Will this idea work or do I need a server?

  6. #6
    Join Date
    Oct 2005
    Location
    Minnesota, U.S.A.
    Posts
    680

    Re: Multiple apps accesing same file on a server

    Quote Originally Posted by bloodfont View Post
    I was thinking of designating a PC (not a server) to receive data from all the other apps, do the calculations, and respond to all the apps. Will this idea work or do I need a server?
    Then the PC IS a server.

    You are talking about having another destination that handles everything properly, then it alone writes the result to the ftp server? That should work.

  7. #7
    Join Date
    May 2012
    Posts
    6

    Re: Multiple apps accesing same file on a server

    Quote Originally Posted by egawtry View Post
    Then the PC IS a server.

    You are talking about having another destination that handles everything properly, then it alone writes the result to the ftp server? That should work.
    If this works I won't need the ftp server anymore. Thanks for your help. This seems to be the way to go.

  8. #8
    Join Date
    Oct 2005
    Location
    Minnesota, U.S.A.
    Posts
    680

    Re: Multiple apps accesing same file on a server

    Seems that you went with modifying the server side anyway.

    Good luck!

  9. #9
    Join Date
    Feb 2005
    Location
    Madrid (Spain)
    Posts
    511

    Re: Multiple apps accesing same file on a server

    Hi.

    You can use MSMQ (or equivalent) to send notifications to the server and receive response in clients. In the server side you can use another exe to process queue menssages and update the file.

    Best regards.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured