curl-users
Re: Is it possible to reuse a curl_easy FTP handle
Date: Mon, 31 Mar 2003 09:20:28 -0500
Hi,
I have the following test code that hangs. I am not sure where the problem
is.
It looks like it is trying to reuse the connection but does not proceed
beyond
what it shown in the verbose output. I would appreciate if someone could
point out what's wrong with the code. The process was hanging and had to
be interrupted. Also, rename after a put does not seem to work.
I am using ftpupload.c and persistant.c as reference for my test code.
I am trying to transfer two files over the same connection, from a VMS
(Compaq C V6.4-008 on OpenVMS Alpha V7.3-1) system to HP-UX
(HP-UX hps02 B.11.00 U 9000/800 500786778 unlimited-user license).
Thanks & Regards,
Raj
*** Code ***
/***************************************************************************
**
* _ _ ____ _
* Project ___| | | | _ \| |
* / __| | | | |_) | |
* | (__| |_| | _ <| |___
* \___|\___/|_| \_\_____|
*
* $Id: ftpupload.c,v 1.1 2001/08/29 07:12:04 bagder Exp $
*/
#include <stdio.h>
#include <unistd.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <curl.h>
/*
* This example shows an FTP upload, with a rename of the file just after
* a successful upload.
*
* Example based on source code provided by Erick Nuwendam. Thanks!
*/
#define LOCAL_FILE "HOLID.DAT"
#define UPLOAD_FILE_AS "holid.dat"
#define REMOTE_URL "ftp://tmgr62ae:ae@hps02.omr.com//62ae/dat/prtdsk/"
UPLOAD_FILE_AS
#define RENAME_FILE_TO "renamef1.txt"
#define UPLOAD_FILE_2 "holi2.dat"
#define REMOTE_URL_2 "ftp://tmgr62ae:ae@hps02.omr.com/" UPLOAD_FILE_2
#define RENAME_FILE_2 "renamef2.txt"
int main(int argc, char **argv)
{
CURL *curl;
CURLcode res;
FILE *ftpfile;
FILE * hd_src ;
int hd ;
struct stat file_info;
off_t fsize;
struct curl_slist *headerlist=NULL;
struct curl_slist *headerlis2=NULL;
char buf_1 [] = "put " UPLOAD_FILE_AS;
char buf_2 [] = "rename " UPLOAD_FILE_AS RENAME_FILE_TO;
char buf_3 [] = "put " UPLOAD_FILE_2;
char buf_4 [] = "rename " UPLOAD_FILE_2 RENAME_FILE_2;
/* get the file size of the local file */
hd = open(LOCAL_FILE, O_RDONLY) ;
fstat(hd, &file_info);
close(hd) ;
fsize = file_info.st_size;
/* get a FILE * of the same file, could also be made with
fdopen() from the previous descriptor, but hey this is just
an example! */
hd_src = fopen(LOCAL_FILE, "r");
/* In windows, this will init the winsock stuff */
curl_global_init(CURL_GLOBAL_ALL);
/* get a curl handle */
curl = curl_easy_init();
if(curl) {
/* build a list of commands to pass to libcurl */
headerlist = curl_slist_append(headerlist, buf_1);
headerlist = curl_slist_append(headerlist, buf_2);
/* enable uploading */
curl_easy_setopt(curl, CURLOPT_UPLOAD, TRUE) ;
curl_easy_setopt(curl, CURLOPT_VERBOSE, TRUE);
curl_easy_setopt(curl, CURLOPT_TRANSFERTEXT, TRUE);
/* specify target */
curl_easy_setopt(curl,CURLOPT_URL, REMOTE_URL);
/* pass in that last of FTP commands to run after the transfer */
curl_easy_setopt(curl, CURLOPT_POSTQUOTE, headerlist);
/* now specify which file to upload */
curl_easy_setopt(curl, CURLOPT_INFILE, hd_src);
/* and give the size of the upload (optional) */
curl_easy_setopt(curl, CURLOPT_INFILESIZE, fsize);
/* Now run off and do what you've been told! */
res = curl_easy_perform(curl);
fclose(hd_src); /* close the local file */
curl_slist_free_all(headerlist);
/******************************************************/
hd_src = fopen(LOCAL_FILE, "r");
headerlis2 = curl_slist_append(headerlis2, buf_3);
headerlis2 = curl_slist_append(headerlis2, buf_4);
/* specify target */
curl_easy_setopt(curl,CURLOPT_URL, REMOTE_URL_2);
/* pass in that last of FTP commands to run after the transfer */
curl_easy_setopt(curl, CURLOPT_POSTQUOTE, headerlis2);
/* now specify which file to upload */
curl_easy_setopt(curl, CURLOPT_INFILE, hd_src);
/* and give the size of the upload (optional) */
curl_easy_setopt(curl, CURLOPT_INFILESIZE, fsize);
/* Now run off and do what you've been told! */
res = curl_easy_perform(curl);
/* always cleanup */
curl_easy_cleanup(curl);
}
curl_global_cleanup();
return 0;
}
*** Output ***
<TRADEMGR>ftpupld
* About to connect() to hps02.omr.com:21
* Connected to hps02.omr.com (192.189.252.33) port 21
> USER tmgr62ae
> PASS ae
* We have successfully logged in
> PWD
* Entry path is '/62ae/traders/tmgr62ae'
> CWD /62ae/dat/prtdsk
> EPSV
> PASV
* About to connect() to hps02.omr.com:58940
* Connecting to hps02.omr.com (192.189.252.33) port 58940
* Connected the data stream with PASV!
> TYPE A
> STOR holid.dat
* Connection #0 left intact
* Re-using existing connection! (#0)
* Connected to (192.189.252.33) port 21
> CWD /62ae/traders/tmgr62ae
Interrupt
=====
On Fri, 28 Mar 2003, Mudumbai, Rajanarayanan wrote:
> I would like to reuse a curl session handle to do multiple FTP transfers
to
> upload files from C. The file names will be different for each transfer
and
> I want to avoid closing and reopening sessions.
>
> I saw an example - persistant.c - that downloads URLs, but is a persistent
> connection possible for multiple FTPs ?
Yes. You can always re-use the easy handle, the library will then re-use
connections as much as it can.
> If so is there a sample C code ?
I don't think so. But I will gladly accept any example sources showing
this...
You basicly just rewrite persistant.c to do uploads instead.
-- Daniel Stenberg -- curl, cURL, Curl, CURL. Groks URLs. ------------------------------------------------------- This SF.net email is sponsored by: ValueWeb: Dedicated Hosting for just $79/mo with 500 GB of bandwidth! No other company gives more support or power for your dedicated server http://click.atdmt.com/AFF/go/sdnxxaff00300020aff/direct/01/Received on 2003-03-31