ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
you have to get hold of the FTP RFC
it typically listens in port 20 and 21.
You have to control the ftp using 20 port and the data comes in 21
there are other methods to receive file just by using port 21 only
which is referred-to as 'passive mode'
Funny I forgot about using wget and wput! However, both of these aren't available on my embedded Linux platform so I'd have to get the source and compile these. I will consider this as an alternative option.
You can only use system() to run a program in a “non-interactive” manner. You have a few ways to proceed (in POSIX C/C++):
Use a library as has been suggested. This does away with calling an external program, and instead you do all ftp work in C(++).
As ChrisAbela has stated, the ftp autologin can be controlled by .netrc file (essentially turning an interactive command into a non-interactive command). So to do this, you might (over)write relevant information to this file using your preferred I/O functions (e.g., iostreams, C stdio, POSIX open()/write()). Once you are done, then use system("ftp") to launch the ftp command (with no arguments); it should autologon and process the init macro without any further input.
Since system() launches a shell to launch your command, you can build a very complicated string which uses the shell to accomplish piping. For example, keeping with ChrisAbela’s approach, you would do something like (if you use C++ strings):
Here you would already have strings for username, etc. Of course, this option may not be very reliable, depending on your ftp client. If precise timing is required (i.e., you can only enter the username after the prompt comes up), then you may get into problems. If, however, your ftp client will accept commands on a buffered input stream (and process them sequentially) which is the way most work (I think), then you can eliminate all the sleep statements and be confident that your code will work.
The next option will only work if your ftp client is not time-sensitive. You can open a pipe to the ftp client with popen("ftp"). Then, successively write the lines you would normally type to the FILE* with fwrite().
The last option (along second) is the only reliable way to call an external ftp client if it is timing sensitive. You will need to open two pipes (with pipe()), then fork(). In the parent process you close the read end of one pipe and the write end of the other. In the child process close the opposite ends, and dup2() the pipes with the the STDIN and STDOUT file descriptors. Then exec ftp in the child. In the parent, you will have a loop which reads from the reading pipe, until it sees a prompt. After this, you can go ahead and write the next line to the writing pipe. The if you write the quit string, the child should exit, and you should get EOF on your attempt to read the pipe, so your loop will exit.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.