coryder
asked on
COMMUNICATING with FTP
i know that fstream allows communication with a file. is there an equivalent class that would allow me to communicate likewise with the ftp program provided by UNIX.
what i'm trying to do is write a program that acts as an interface to ftp. it grabs info from a file, and then manipulates the ftp program accordingly. meanwhile, i don't want anything printed on screen. so i want to execute "ftp", and then get the response "ftp>" into a variable, and check it, then send back "open", and expect the response "(to):"...and so on.
i've tried using C function:
popen("ftp", "rt");
but the problem with this is that i need to read back the response, i.e. "ftp>" when i send "ftp". and i can get "ftp>" with something like: fgets, but fgets doesn't know when to stop reading because after "ftp>" nothing would be sent because ftp is waiting for a command, but fgets doesn't know that and it keeps trying to read. i think i need to read nonblocked data or something.....not too sure.
anyway, help =) a C++ solution that replaces the use of popen() would be most appreciated, and preferred. but any other solution is welcomed.
thanks.
what i'm trying to do is write a program that acts as an interface to ftp. it grabs info from a file, and then manipulates the ftp program accordingly. meanwhile, i don't want anything printed on screen. so i want to execute "ftp", and then get the response "ftp>" into a variable, and check it, then send back "open", and expect the response "(to):"...and so on.
i've tried using C function:
popen("ftp", "rt");
but the problem with this is that i need to read back the response, i.e. "ftp>" when i send "ftp". and i can get "ftp>" with something like: fgets, but fgets doesn't know when to stop reading because after "ftp>" nothing would be sent because ftp is waiting for a command, but fgets doesn't know that and it keeps trying to read. i think i need to read nonblocked data or something.....not too sure.
anyway, help =) a C++ solution that replaces the use of popen() would be most appreciated, and preferred. but any other solution is welcomed.
thanks.
Standard C++ does not provide any support for sockets/HTTP/FTP. What platform are you using? What C++ compiler are you using?
ASKER
i'm developing for Interix and using compiler:
gcc version cygnus-2.7.2-970404
gcc version cygnus-2.7.2-970404
you need berkeley sockets . socket() , send() , recv() , connect() , accept() and close() . They are all documented in the Windows Sockets API ( Windows ) , with the exception that in unix you don't need WSAStartup or WSACleanup . Also the socket() supports more socket types .
ASKER
sorry, WxW, i don't quite understand how berkley sockets help me...could you explain more if it's not too much trouble....
There are 2 options I think.
1. drive ftp directly by executing a dynamically generated script
and reading an output file... pseudo code could be...
#define SCRIPT_TEMPLATE "echo \"open %s\n\
user %s %s\n\
get %s\n\
quit" | ftp -v -n -i 2>&1 >> ftp_log
char script[2000]
sprintf(script,SCRIPT_TEMP LATE,site, username,p assword,fi le);
//Now write the string script to a file..
//now exec() the file (which is a bash script)
//now open the file ftp_log to see what happened.
2. For a more sophisticated app (which is probably what
you need) look at the API provided by:
http://www.ncftp.com/libncftp/
1. drive ftp directly by executing a dynamically generated script
and reading an output file... pseudo code could be...
#define SCRIPT_TEMPLATE "echo \"open %s\n\
user %s %s\n\
get %s\n\
quit" | ftp -v -n -i 2>&1 >> ftp_log
char script[2000]
sprintf(script,SCRIPT_TEMP
//Now write the string script to a file..
//now exec() the file (which is a bash script)
//now open the file ftp_log to see what happened.
2. For a more sophisticated app (which is probably what
you need) look at the API provided by:
http://www.ncftp.com/libncftp/
sorry I misunderstood i thought you wanted to make a ftp program .
ASKER
are there free FTP APIs out there? cuz i'm not looking to purchasing any.... sorry i didn't specify this before...
ASKER
thanks anyway WxW =)
If you are looking on how to make a FTP program , you must first read the FTP document , then use the functions i told you to do the transfers .
I don't think there are any FTP APIs which are *free* .. However I do know the ones which you'll have to buy - and the vendor is FTP Software (No pun intended !) - why don't you check out www.ftp.com (They are now known as Netmanage)
ASKER
thanks for all the help all. i found a gnu extension called procbuf, a derivation from streambuf which might do what i want. i'm really not looking to writing an FTP program, so i'll check that out first. thanks.
Ftp APIs are available on Windows but not on Unix. However You can find TFTP code by Richard Stevens.
This not the way to do that. You must use sockets for connecting to an ftp server and work with it. If you have a sockets library for example winsock you must connect to a server (connect())
and send ftp commands (send()) and you can get the replies with recv().
Look a the winsock or other socket libraries for details.
A note. When you work woth ftp you need two connections, a TCP connection for control and a UDP connection for data.
The second connection is needed for transfering files o LIST results.
You can negotiate the port for transferring this information with the command PORT.
If you need more information about ftp commands you can look the FTP especification or connect to an ftp site with a telnet and try HELP.
CU.
P.S. I can send you an example program if you need it.
and send ftp commands (send()) and you can get the replies with recv().
Look a the winsock or other socket libraries for details.
A note. When you work woth ftp you need two connections, a TCP connection for control and a UDP connection for data.
The second connection is needed for transfering files o LIST results.
You can negotiate the port for transferring this information with the command PORT.
If you need more information about ftp commands you can look the FTP especification or connect to an ftp site with a telnet and try HELP.
CU.
P.S. I can send you an example program if you need it.
ASKER
thx abusimbel but i was hoping to device an interface that sits on top of an existing FTP program....i'm trying fork() pipe() and exec() right now, which is closer to what i'm looking for. thanks for the suggestion tho.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
thanks. that should be very helpful!