Thread: FTP programming in C

  1. #1
    Registered User
    Join Date
    Jun 2005
    Posts
    108

    FTP programming in C

    i dont know if this is the right board or the networking one but iŽll post here anyway.

    i have two options for a program i just created.

    my program creates a folder every day with 24 files inside and has to be transfered through FTP to another computer and website.

    the options i have are to:

    1) create a FTP transfer section inside my current program to upload the files or

    2) use windows scheduler to open a small program i will make to send the files every day, at 1:00am or so.

    i wanted to know which would be an easier option.

    P.s. the ftp section HAS to be written in C even though i know there are easier methods of doing it.....

  2. #2
    Registered User mitakeet's Avatar
    Join Date
    Jun 2005
    Location
    Maryland, USA
    Posts
    212
    There are libraries you can link to that will give you FTP capability. Keep in mind that the FTP protocol exchanges username and password in the clear (as is all the data being transmitted), so be sure to use a highly restricted account.

    Free code: http://sol-biotech.com/code/.

    It is not that old programmers are any smarter or code better, it is just that they have made the same stupid mistake so many times that it is second nature to fix it.
    --Me, I just made it up

    The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man.
    --George Bernard Shaw

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    108
    thanks.

    but which approach would you recommend!?

  4. #4
    Registered User mitakeet's Avatar
    Join Date
    Jun 2005
    Location
    Maryland, USA
    Posts
    212
    It is six of one, half dozen of another. If you want absolute control of the timing, embed it in your program. If you can be positive that the time scheduled for the file transfer will never coincide with the time that the files are being created/modified, then the scheduler works fine. You still have to write the code either way (unless you are going to script the FTP, which is fine on an intranet with a dumbed down account).

    Free code: http://sol-biotech.com/code/.

    It is not that old programmers are any smarter or code better, it is just that they have made the same stupid mistake so many times that it is second nature to fix it.
    --Me, I just made it up

    The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man.
    --George Bernard Shaw

  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    108
    no probs, iŽll try approach two because i dont need strict control.

    although i heard it hard to do?

    P.s. iŽm a novice but have programmed serial ports and so on....

  6. #6
    Registered User mitakeet's Avatar
    Join Date
    Jun 2005
    Location
    Maryland, USA
    Posts
    212
    Say you do your file processing at 10 AM and it is expected to last 30 minutes. Then you do your upload at 10 PM and it is expected to take 5 minutes. Should never be any chance of overlap, right? That may be true 99.9% of the time, but weird things happen and occasionally you may see overlap. You can, of course, use signal files, etc. to create some sort of flow control (research IPC (inter process communication) for more about those sorts of headaches), but all that goes away if you have a single program that does it all. Of course, that won't save you if that single program gets hung (believe me, it will happen!) and is still 'running' the next time the task is scheduled to run. You can easily wind up with 80-90% of your code just being there to handle wacko error conditions that happen once a year, but that is what being a paid professional is all about. I would make the unified program and use some sort of OpenSSL library for encrypted communication (being the paranoid sort), coupled with either a database or attempts at file signaling to detect errors and/or concurrent running. A lot depends on how much learning curve you have to go through and how time sensitive the project is. As you say you are a newbie, I would start out with a simple FTP script can call it via system() (system blocks until the program it calls returns) and a simple file (maybe put the process PID and start time in the file, delete it when complete) for dealing with concurency. If it turns out that is good enough, move on to the next project. If not, at least something is working while you bore through the learning curve to try and get your FTP code working.

    Free code: http://sol-biotech.com/code/.

    It is not that old programmers are any smarter or code better, it is just that they have made the same stupid mistake so many times that it is second nature to fix it.
    --Me, I just made it up

    The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man.
    --George Bernard Shaw

  7. #7
    Registered User
    Join Date
    Jun 2005
    Posts
    108
    thanks alot man.

    alot of what you said is far too complex for this mind!!
    iŽll start off slow and work my way through. what i could also do is to have a lag of about 1 day before the files are actually uploaded into the FTP site. so i never overlap.

    oh, i am a "paid professional" but get paid so little that i might as well say iŽm doing it for fun!!


    thanks

  8. #8
    Registered User mitakeet's Avatar
    Join Date
    Jun 2005
    Location
    Maryland, USA
    Posts
    212
    Do the FTP script thing, it is quite painless. The use of a file for 'locking' does have problems (research 'race conditions' if you are bored), but they are typically on the milisecond or less timescale (though, as a hacker, you can stretch that interval out to a few seconds or longer with some clever tricks), so if you are going to run once a day that should not be any potential problem. Just delete the file when your program has completed cleanly and check for the presense of the file first-thing on startup and exit if it see it. Why I suggest putting in the PID and the start time is so that when you go to diagnose the error you can find out if the program is still running or not. Note that you will have to manually delete that file if your program ever crashes! I often have my programs email me a status report when they are finished so I have some clew on what the problems are. The problem with that is you develop a reflex of just deleting the emails, then forget if you recieved it. Ultimately, though, there is only so much automation you can do, at some point a human has to evaluate if the program output is correct.

    Free code: http://sol-biotech.com/code/.

    It is not that old programmers are any smarter or code better, it is just that they have made the same stupid mistake so many times that it is second nature to fix it.
    --Me, I just made it up

    The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man.
    --George Bernard Shaw

  9. #9
    Registered User
    Join Date
    Jun 2005
    Posts
    108
    ok, iŽll try that...will keep you posted...

    thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to program in unix
    By Cpro in forum Linux Programming
    Replies: 21
    Last Post: 02-12-2008, 10:54 AM
  2. FTP problems...
    By gcn_zelda in forum Tech Board
    Replies: 9
    Last Post: 08-03-2004, 11:05 PM
  3. FTP and Ident Server :: Winsock
    By kuphryn in forum Networking/Device Communication
    Replies: 2
    Last Post: 03-13-2004, 08:16 PM
  4. [C++] FTP client problem (winsock)
    By Niekie in forum Networking/Device Communication
    Replies: 2
    Last Post: 10-19-2003, 09:23 AM
  5. FTP Server :: Winsock
    By kuphryn in forum Windows Programming
    Replies: 2
    Last Post: 10-03-2002, 07:14 PM