ftplib-分析

由于要在VC6下完成FTP下载文件的功能,今天从网上下载了ftplib-3.1-1.zip并解开,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);
4)对链中的每个节进行处理,就是分别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 <winsock.h>
#include <io.h>
#include <stdio.h>
#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;
}

FTP Library Routines Release 4.0 Thomas Pfau (tfpfau@gmail.com) June 7, 2013 This package implements a callable interface to FTP. The FTP protocol is specified in RFC 959. The library has been tested on linux, OpenVMS and Windows NT. It should also work without major modification on other POSIX systems. All programs using the library should include ftplib.h. FTP开源库。 Miscellaneous Functions FtpInit() - Initialize the library FtpSite() - Send a 'SITE' command FtpLastResponse() - Retrieve last server response FtpSysType() - Determine remote system type FtpSize() - Determine size of remote file FtpSizeLong() - Determine size of remote file FtpModDate() - Determine modification time of file FtpSetCallback() - Establish a callback function FtpClearCallback() - Remove a callback function Server Connection FtpConnect() - Connect to a remote server FtpLogin() - Login to remote machine FtpQuit() - Disconnect from remote server FtpOptions() - Set Connection Options Directory Functions FtpChdir() - Change working directory FtpMkdir() - Create a directory FtpRmdir() - Remove a directory FtpDir() - List a remote directory FtpNlst() - List a remote directory FtpCDUp() - Change to parent directory FtpPwd() - Determine current working directory File to File Transfer FtpGet() - Retreive a remote file FtpPut() - Send a local file to remote FtpDelete() - Delete a remote file FtpRename() - Rename a remote file File to Program Transfer These routines allow programs access to the data streams connected to remote files and directories. FtpAccess() - Open a remote file or directory FtpRead() - Read from remote file or directory FtpWrite() - Write to remote file FtpClose() - Close data connection Utilities qftp - Command line ftp utility
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值