LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   help: Using FTP client from C/C++ code (https://www.linuxquestions.org/questions/programming-9/help-using-ftp-client-from-c-c-code-511569/)

Rostfrei 12-18-2006 06:05 AM

help: Using FTP client from C/C++ code
 
Hello!

I searched the forum for the question and didn't find satisfactory answer. I want to use FTP client code from my C/C++ application. What is the easiest way to do so? I found description of ftp module on Inets Reference Manual http://www.erlang.org/doc/doc-5.5.2/...ion_frame.html. I don't know where does this module come from, how it is loaded into the kernel, is it working trough some ioctl? Is it possible to use it from the code?

Please, all I need is some meaningful direction to use FTP client from the code.

Best regards,

taylor_venable 12-18-2006 11:06 AM

That's an Erlang module; probably not what you want if you're looking to use C or C++. (Unless you're trying to write some C code as part of an Erlang project?)

Do you have to use the interactive features of FTP in your application, or do you just need to download a file? If it's the latter, you can call an external process like "wget" or "fetch" to download the file for you, then do whatever you need to to the file that results.

Rostfrei 12-18-2006 11:43 AM

I'm working on the embedded device which would be upgraded with the file downloaded from some FTP server. It would first be downloaded to the memory and then written to the flash trough /dev/mtdblock or file system (I don't have enough flash to store it directly to the flash, so I have to be sure I can download file properly to the RAM before I delete current version from the flash and write new one). It would also be able to upload it's backup to the FTP server.

I will need the similar interfaces from the C++ code for the DNS client, TFTP client, SNTP client, ICMP, etc.

I'm new to the Linux. I previously worked on the VxWorks system where all this features were already provided.

Best regards,

Denes 12-20-2006 07:48 PM

DNS client is provided using the gethostbyname function. This function is also in VxWorks.

FTP client can be controlled via popen which you can use to spawn a process for the ftp client and a pipe that you can send commands to it. I have done the equivalent with a bash script so it should work in a C/C++ program. Check out wget or fetch though, like the previous poster indicates. You can use fork and one of the exec functions to pass command line parameters to them.

I don't know about SNTP, worst case is you may have to find an implementation but it should not be difficult.

TFTP client I am also not sure about. You may be able to do the same as with FTP.

ICMP is a low level error protocol, you should not have to worry about it, it should just work.

You do know that Linux is not hard real time? You may get some unexpected delays occasionally in your system. To solve this there are some real time extensions to Linux. Or you could try LynxOS, but it doesn't share the same driver model as Linux so you may have to port device drivers.

Rostfrei 12-21-2006 01:17 AM

Real time issue is really not so important here as it's about SIP phone application.

gethostbyname is not enough for me in some cases because I need to resolve the domain name into IP address and port as conforms to "RFC 3263 - Session Initiation Protocol (SIP): Locating SIP Servers"

What do you think about ICMP should just work? Is there some "ping" function in the net library I can use? I had something like ping from the code on VxWorks. I called the function and it returned the result (it was blocking function). I need it sometimes to check the presence of the network devices.

About FTP, TFTP client. I'm totally new to the popen and piping and sending command to the FTP client from the code. Can you direct me on some page where I can see some code? How can I download the file to the RAM first and not directly to the disk? I heard something about RAM disk (here I would have to do mounting from the code?).

Best regards,

Denes 12-21-2006 03:55 PM

I don't know how to get a port for a protocol over DNS so I couldn't help you there. Possibly spawning the nmap program can help you.

int retval = system("ping -c 1 192.168.0.1");

will do a system call and return a value for ping. This is just one use of ICMP.

Download Advanced Linux Programming (there is a free pdf available) and read the section 5.4.4 on popen. This has easy source code in it. I would recommend reading the whole book, it will enlighten you.

Example of script with ftp, you would use popen, and use fprintf on the descriptor instead. Normally I use variables here.

# Run FTP as a client, opening server at IPAddress
ftp -n 192.168.0.1 <<END_SCRIPT
# Send user name
quote USER anonymous
# Send password
quote PASS anything
# Change to binary mode
binary
# Change to directory where file is
cd FileDirectory
# Get the file
get File
# Exit FTP
quit
# End of FTP script portion
END_SCRIPT

Programming in Linux can be different from VxWorks in that VxWorks gives you C functions for protocols like TFTP and SNTP, whereas in Linux, you spawn applications (processes) and pass in parameters to do this for you.

I have never used a RAM disk in Linux. It may work but you may also want to look at shared memory, output redirection, fifo's, or pipes to see if one of these techniques will work with ftp.


All times are GMT -5. The time now is 12:02 AM.