I want to run a ftp from c++ asynchronously perhaps using bash
I want to run a ftp from c++ asynchronously. For example:
Using the following bash command: //ftp -in <ip address> <<HERE //user <user> <password> //put <filename> //bye //HERE Inserted into a c++ system call: system ("ftp -in 1.2.3.4 <<HERE\nuser username password\nput filename\nbye\nHERE&"); Note I want the ftp to run asynchronously thus at the end of the bash command I have '&'. Will using $? give the the result of the ftp transfer, thus because of the '&' does $? still work? For example can I then just do the following: system("if [ $? -eq 0 ]; then\n do something\nfi") Is there a better way? How do I know the process has finsihed, do I have to monitor the process? Should the ftp command just be in a phtread? |
Hello DEF. :)
Do you want the compiled C++ executable to wait for the FTP process to complete or to come back later and see what happened? AFAIK, each system( ) call starts a new shell (which may or may not exit, depending on what you make it do); it's not like it there's one shell that you can keep dipping back into. Thus you can't use Code:
system("if [ $? -eq 0 ]; then\n do something\nfi") Code:
system ("ftp -in 1.2.3.4 <<HERE\nuser username password\nput filename\nbye\nHERE&"); As with all asynchronous processes you need to time-coordinate between them; yes, you will have to "monitor the process" to see if it has finished before you can check how it went. I don't C++ but guess the system() call returns the PID of the process it initiated. Presumably there's another function you can call from C++ to query a process by giving its PID. Beware the PID may be re-used! Best Charles |
|
P.S. I don't know about phtreads but, if you can multi-thread then setting up a thread to run the system call that starts the FTP work and not using "&" would be a neat way to pick up the exit status, from the system()'s return value.
Asynchronous processes are non-trivial to work with -- way easier to go synchronous if you have the option. |
catkin: Your comments make sense so I have done what you said and used pthreads.
bigearsbilly: I wasn't sure how I am suppose to use this configuration file to solve my problem? |
All times are GMT -5. The time now is 06:58 AM. |