参考库

  • libftp (though it's in C)
  • ftplib (again, looks like C)
  • libCurl seems to have FTP capabilities.
  • ace

源码:main.c

#include <stdio.h>
#include <string.h>#include <curl/curl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#ifdef WIN32
#include <io.h>
#else
#include <unistd.h>
#endif/** 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      "main.c"
#define UPLOAD_FILE_AS  "wkl.c"
#define REMOTE_URL      "ftp://127.0.0.1/"  UPLOAD_FILE_AS
#define RENAME_FILE_TO  "haaaa.c"
#define USERPASSWD      "nobody:lampp"/* NOTE: if you want this example to work on Windows with libcurl as aDLL, you MUST also provide a read callback with CURLOPT_READFUNCTION.Failing to do so will give you a crash since a DLL may not use thevariable's memory when passed in to it from an app like this. */
static size_t read_callback(void *ptr, size_t size, size_t nmemb, void *stream)
{curl_off_t nread;/* in real-world cases, this would probably get this data differentlyas this fread() stuff is exactly what the library already would doby default internally */size_t retcode = fread(ptr, size, nmemb, stream);nread = (curl_off_t)retcode;fprintf(stderr, "*** We read %" CURL_FORMAT_CURL_OFF_T" bytes from file\n", nread);return retcode;
}int main(void)
{CURL *curl;CURLcode res;FILE *hd_src;struct stat file_info;curl_off_t fsize;struct curl_slist *headerlist=NULL;static const char buf_1 [] = "RNFR " UPLOAD_FILE_AS;static const char buf_2 [] = "RNTO " RENAME_FILE_TO;/* get the file size of the local file */if(stat(LOCAL_FILE, &file_info)) {printf("Couldnt open '%s': %s\n", LOCAL_FILE, strerror(errno));return 1;}fsize = (curl_off_t)file_info.st_size;printf("Local file size: %" CURL_FORMAT_CURL_OFF_T " bytes.\n", fsize);/* get a FILE * of the same file */hd_src = fopen(LOCAL_FILE, "rb");/* 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);curl_easy_setopt(curl, CURLOPT_USERPWD, USERPASSWD);    /* we want to use our own read function */curl_easy_setopt(curl, CURLOPT_READFUNCTION, read_callback);/* enable uploading */curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L);/* 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_READDATA, hd_src);/* Set the size of the file to upload (optional).  If you give a *_LARGEoption you MUST make sure that the type of the passed-in argument is acurl_off_t. If you use CURLOPT_INFILESIZE (without _LARGE) you mustmake sure that to pass in a type 'long' argument. */curl_easy_setopt(curl, CURLOPT_INFILESIZE_LARGE,(curl_off_t)fsize);/* Now run off and do what you've been told! */res = curl_easy_perform(curl);/* clean up the FTP commands list */curl_slist_free_all (headerlist);/* always cleanup */curl_easy_cleanup(curl);}fclose(hd_src); /* close the local file */curl_global_cleanup();return 0;
}

编译:

gcc -g main.c -o curl -lcurl

运行:

 ./curl
Local file size: 1762 bytes.
*** We read 1762 bytes from file

参考地址:

http://curl.haxx.se/libcurl/c/ftpupload.html

http://wanghaifeng.iteye.com/blog/628630

转载于:https://www.cnblogs.com/wangkangluo1/archive/2012/07/01/2571583.html

libcurl 客户端实例相关推荐

  1. java http 下载网页代码_Java下http下载文件客户端和上传文件客户端实例代码

    Java下http下载文件客户端和上传文件客户端实例代码 发布于 2021-1-14| 复制链接 摘记: 一.下载客户端代码 ```java package javadownload; import ...

  2. java socket 编程 客户机服务器_Java Socket编程服务器响应客户端实例代码

    通过输入流来读取客户端信息,相应的时候通过输出流来实现. 服务端类的代码: import java.io.BufferedReader; import java.io.IOException; imp ...

  3. VoIP 软电话客户端实例

    VoIP 软电话客户端实例共享 博主在一家企业的IT部门工作,经常要跟国外的同事开跨国电话会议,以前用自己的手机打国际长途,话费太贵了,我自己做了一个Windows版本的软电话客户端,再去找了一条运营 ...

  4. memcached win64位服务端安装和java客户端实例

    项目开发中需要用到memcached缓存记录下来相关操作方便日后复习,如果有错误或遗漏请留言. memcached服务端安装 下载安装包 下载地址 32位系统 1.4.4版本:http://stati ...

  5. java服务端代码_Java Socket编程服务器响应客户端实例代码

    通过输入流来读取客户端信息,相应的时候通过输出流来实现. 服务端类的代码: import java.io.BufferedReader; import java.io.IOException; imp ...

  6. MQTT协议学习:3、MQTT客户端实例

    MQTT协议学习:3.MQTT客户端实例 文章目录 MQTT协议学习:3.MQTT客户端实例 1. 前言 2. Paho MQTT (1). Go客户端实例 (2). Python客户端实例 (3). ...

  7. 接入新浪、腾讯微博和人人网的Android客户端实例 接入新浪、腾讯微博和人人网的Android客户端实例...

    做了个Android项目,需要接入新浪微博,实现时也顺带着研究了下腾讯微博和人人网的Android客户端接入,本文就跟大家分享下三者的Android客户端接入方法. 一.实例概述 说白了,接入微博就是 ...

  8. voip 软电话 客户端实例 SIP

    这几天研究编译了很多份软电话的源码,就发现这个份源码是可用的.http://download.csdn.net/detail/ljmscsq/753817 注意必须用vc6.0编译. 编译过程 参考这 ...

  9. socket 服务器浏览器与服务器客户端实例

    一.服务器与浏览器 // 取得本机的loopback网络地址,即127.0.0.1             IPAddress address = IPAddress.Loopback;        ...

最新文章

  1. 2021年大数据环境命令(一):常用命令汇总
  2. 负载均衡探测器lbd
  3. python代码壁纸-python实现壁纸批量下载代码实例
  4. free malloc
  5. linux一直用户身份验证失败,linux – chsh:PAM身份验证失败
  6. 你好,C++(2)1.3 C++世界版图1.4 如何学好C++
  7. 做数学题比统一世界更爽,你会怎么做呢?
  8. java collectiongroup 类_Java中的collection集合类型总结
  9. 一步一步学Silverlight 2系列(27):使用Brush进行填充
  10. Web 实时推送技术如何弥补 HTTP 协议的缺陷? | 技术头条
  11. cad填充图案乱理石_CAD图案填充应该这么操作!简单又高效!!!1分钟就能学会...
  12. 使用Movavi Video Editor如何做局部的影片放大特效
  13. WebRAY权小文:产品就是工程师的尊严
  14. Qt获取本机硬盘序列号,不受IDE硬盘与SCSI硬盘类型影响
  15. STM32 利用cubemx配置正点原子4.3寸TFT-LCD 驱动芯片NT35510
  16. Mysql使用Key/Value方式存储动态扩展字段、对象与HashMap的相互转化
  17. matplotlib cmap取值
  18. 怎么恢复移动硬盘数据
  19. Lua实现三目运算符
  20. 美国是一个愚蠢而落后的国度--大家千万别去

热门文章

  1. 2022-2028年中国EBA树脂(乙烯丙烯酸丁酯)产业竞争现状及发展前景规划报告
  2. 2022-2028年中国交通建设PPP模式深度分析及发展战略研究报告(全卷)
  3. python pandas 如何找到NaN、缺失值或者某些元素的索引名称以及位置,np.where的使用
  4. python yield 和 return 对比分析
  5. Django celery6.4
  6. Centos7下安装MongoDB
  7. Cocos Creator里cc.tween的stopAllActions() 和 repeatForever的用法
  8. Spinner 使用的使用 报错:ArrayAdapter requires the resource ID to be a TextView
  9. 一个http的Post请求问题,unable to resolve host 我的域名:no address associated with hostnam...
  10. nginx检查配置文件语法是否正常,需要检查主配置文件