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.
coryderAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

chensuCommented:
Standard C++ does not provide any support for sockets/HTTP/FTP. What platform are you using? What C++ compiler are you using?
0
coryderAuthor Commented:
i'm developing for Interix and using compiler:
gcc version cygnus-2.7.2-970404
0
WxWCommented:
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 .
0
Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

coryderAuthor Commented:
sorry, WxW, i don't quite understand how berkley sockets help me...could you explain more if it's not too much trouble....
0
IexpertCommented:
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_TEMPLATE,site,username,password,file);

    //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/



0
WxWCommented:
sorry I misunderstood i thought you wanted to make a ftp program .
0
coryderAuthor Commented:
are there free FTP APIs out there?  cuz i'm not looking to purchasing any.... sorry i didn't specify this before...
0
coryderAuthor Commented:
thanks anyway WxW =)
0
WxWCommented:
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 .
0
fstabCommented:
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)
0
coryderAuthor Commented:
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.
0
basantCommented:
Ftp APIs are available on Windows but not on Unix. However You can find TFTP code by Richard Stevens.
0
abusimbelCommented:
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.
0
coryderAuthor Commented:
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.
0
TallBoyCommented:
If you want to drive FTP (the utility, not the process), which is how I read your question, then you will want to grab a copy of either Expect (http://expect.nist.gov) or Tcl (both free), along with the respective O'reily book (not free :( ).

There are two approaches to doing what you want to do, with various pros and cons.

1) Automate FTP, the utility
   PRO: You don't have to recode the bugger.
   CON: You're going to spend a lot of time debugging, because you will have to anticipate every possible error, based upon ascii status messages.  You also won't have as much control.

2) Write your own FTP for your code.
   PRO: You have total control with error-handling on both ends, recovery and so on.  You don't have to reproduce the entire FTP utility (& reinvent the wheel).
   CON: You'll have to learn how to do it.

If you're going to do 1, then Expect or TCL is the way to go, definitely.  You then call your Expect/Tcl script from the C program.

If you DON'T do Expect/Tcl, then you're going to end up writing a "lite Expect," by basically streaming characters to and from FTP -- this will take a lot of coding.

The nice thing is 1) you learn Expect/Tcl, or 2) you learn how to code FTP, so then you'll have more k-nowl-edge!

Good luck - let us know how it worked.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
coryderAuthor Commented:
thanks.  that should be very helpful!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.