Chinaunix首页 | 论坛 | 博客
  • 博客访问: 4195376
  • 博文数量: 776
  • 博客积分: 13014
  • 博客等级: 上将
  • 技术积分: 10391
  • 用 户 组: 普通用户
  • 注册时间: 2010-02-22 17:00
文章分类

全部博文(776)

文章存档

2015年(55)

2014年(43)

2013年(147)

2012年(20)

2011年(82)

2010年(429)

分类: LINUX

2010-11-09 17:18:29

  ftplib-3.1-1中包含ftplib.dll的源代码项目以及一个DEMO项目(qftp)。

  在qftp项目中我设置了调试命令行:get 127.0.0.1 -l tianfu -p 123456 -w -v 1 ab,意图很简单,把FTP服务器上的ab目录下的所有文件给DOWNLOAD下来。

  GET总过程:

  (1)初始化SOCKET,就是调用WSAStartup

  FtpInit();

  (2)连接FTP服务器以及身份验证:

  ftp_connect();

  (3)列出ab目录下的所有文件并放到一个结构链中,此结构里面包含文件名、文件大小以及接下的指针;

                netbuf *dir;
                char *buf;
                if (!FtpAccess(fnm, FTPLIB_DIR, FTPLIB_ASCII, conn, &dir))
                {
                    fprintf(stderr,"error requesting directory of %s"n%s"n",
                    fnm, FtpLastResponse(conn));
                    return;
                }
                buf = malloc(DIRBUF_SIZE);
                while (FtpRead(buf, DIRBUF_SIZE, dir) > 0)
                {
                    struct REMFILE *f;
                    char *p;
                    f = (struct REMFILE *) malloc(sizeof(struct REMFILE));
                    memset(f,0,sizeof(struct REMFILE));
                    f->next = filelist;
                    p = strchr(buf,'"n');
                    if (p)
                        *p = '"0';
                    f->fnm = strdup(buf);
                    filelist = f;
                }
                free(buf);
                FtpClose(dir);

  对链中的每个节进行处理,就是分别FTP每个文件

FtpGet(f->fnm,f->fnm,mode,conn);

  在这个过程中,有一个方法非常实用,就是FtpOptions:

                FtpOptions(FTPLIB_CALLBACK, (long) log_progress, conn);
                FtpOptions(FTPLIB_IDLETIME, (long) 1000, conn);
                FtpOptions(FTPLIB_CALLBACKARG, (long) f, conn);
                FtpOptions(FTPLIB_CALLBACKBYTES, (long) fsz, conn);

  此功能模仿了定时器和回调的功能,每秒钟进行显示某文件的下载进度。

  若是下载某个文件,调试命令行改为get 127.0.0.1 -l tianfu -p 123456 -w -v 1 a.pdf就可以了。

  自己简单化了下程序,其实这样就可以了:

#include
#include
#include
#include "ftplib.h"

static netbuf *conn = NULL;

void ftp_connect()
{
    if (conn)
        return;
   
    if (!FtpConnect("192.168.8.184",&conn))
    {
        exit(1);
    }
    if (!FtpLogin("tianfu","123456",conn))
    {
        exit(1);
    }
}

int main(int argc, char *argv[])
{
    FtpInit();
    ftp_connect();
    FtpOptions(FTPLIB_CALLBACK, (long) NULL, conn);
    FtpGet("c:""a.pdf","a.pdf",'I',conn);
    FtpGet("c:""abc.txt","abc.txt",'I',conn);
    FtpGet("c:""abc.exe","abc.exe",'I',conn);
    FtpGet("c:""FTPLib.dll","FTPLib.dll",'I',conn);
    if (conn)
        FtpClose(conn);
    return 0;
}

阅读(1254) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~