curl-library
upload connection problem with libcurl
Date: Mon, 20 Oct 2008 12:04:53 +0800
Hi,everyone.
I could not connect to my Ubuntu SSH server using libcurl to upload files,while sftp get file works fine(converting demo ftpget.c to sFTP).I know something must be wrong,but where?
this is the code:
static size_t read_callback(void * pBuffer, size_t size, size_t nmemb, void * hFile)
{
DWORD dwNumberOfBytesRead = 0;
BOOL bResult = ReadFile( (HANDLE) hFile, pBuffer, size * nmemb, &dwNumberOfBytesRead, NULL);
return dwNumberOfBytesRead;
}
int my_curl_debug_callback (CURL * objCurl, curl_infotype objT, char * lpszText, size_t uTextSize, void * pPointer)
{
if (objT == CURLINFO_TEXT)
{
USES_CONVERSION;
char * lpszDebugMessage = (char *) alloca(uTextSize + 2);
sprintf(lpszDebugMessage, "%s\0", lpszText);
OutputDebugString(A2CT(lpszDebugMessage));
}
return 0;
}
bool Upload(/*[in]*/ CString bsFilename,
/*[in]*/ CString bsFilePath,
/*[in]*/ CString bsFtpSitePath,
CString bsUserPassword)
{
USES_CONVERSION;
HANDLE hFile = NULL;
char * lpszCurlErrorBuffer[CURL_ERROR_SIZE];
CURLcode nCurlResult = CURL_LAST;
struct stat file_info;
DWORD dwLastError;
/*open a file*/
{
int hd ;
hd = open(bsFilePath, O_RDONLY) ;
fstat(hd, &file_info);
close(hd) ;
/* get a FILE * of the same file*/
hFile = fopen(bsFilePath, "rb");
if(!hFile)
{
dwLastError = GetLastError();
printf("open file failed!\n%s\nDiscribe:%s",bsFilePath, dwLastError);
curl_global_cleanup();
return false;
}
else
printf("open file OK!\n%s\n",bsFilePath);
}
/*begin init*/
curl_global_init(CURL_GLOBAL_ALL);
CURL * hCurl = curl_easy_init();
if (!hCurl)
{
curl_global_cleanup();
printf("CURL-Library easy_init failed \n");
return false;
}
printf("CURL-Library init OK!\n");
/*begin easy_handle*/
{
struct curl_slist * headerlist = NULL;
CString command = "RNFR ";
CString CommandBuffer = command + bsFilename;
/* enable error buffer */
curl_easy_setopt(hCurl, CURLOPT_ERRORBUFFER, lpszCurlErrorBuffer) ;
curl_easy_setopt(hCurl, CURLOPT_VERBOSE , 1);
curl_easy_setopt(hCurl, CURLOPT_DEBUGFUNCTION, my_curl_debug_callback);
/* enable uploading */
curl_easy_setopt(hCurl, CURLOPT_UPLOAD, TRUE) ;
/* specify target */
curl_easy_setopt(hCurl, CURLOPT_URL, bsFtpSitePath);
printf("setopt remote_url:%s\n",bsFtpSitePath);
/* pass commands to libcurl list */
//Does sFTP need this command?
headerlist = curl_slist_append(headerlist, CommandBuffer);
/* read function */
curl_easy_setopt(hCurl, CURLOPT_READFUNCTION, read_callback);//read back file size
/* now specify which file to upload */
curl_easy_setopt(hCurl, CURLOPT_READDATA, hFile);
curl_easy_setopt(hCurl, CURLOPT_USERPWD, bsUserPassword);
/* Now do perform! */
printf("Performing now...\n");
nCurlResult = curl_easy_perform(hCurl);
/* clean up the FTP commands list */
curl_slist_free_all (headerlist);
/* always cleanup */
curl_easy_cleanup(hCurl);
}
CloseHandle(hFile);
curl_global_cleanup();
if (nCurlResult == CURLE_OK)
return true;
else
{
printf("sFTP operation failed. error: %s\n", lpszCurlErrorBuffer);
return false;
}
}
int main()
{
CString Filename,Filepath,sFtppath,userpsw;
Filename = "SSH and SFTP in C++.txt";
Filepath = "D:\\SSH and SFTP in C++.txt";
sFtppath = "sftp://192.168.1.66/home/quincy/Software";
userpsw = "quincy:******";
//printf("%s\n",Filename);
Upload(Filename,Filepath ,sFtppath,userpsw);
return 0;
}
Compling with VC6,Winxp.
The nCurlResult paramenter turnback connect failed.
What's wrong? Any suggestion?
-------------------------------------------------------------------
新浪空间——与朋友开心分享网络新生活!(http://space.sina.com.cn/ )
Received on 2008-10-20