项目中有用到ftp对远程文件进行管理,这里整理一下,主要是通过CFNetwork来实现的。
首先对ftp进行一个小得概述,后面会通过抓包工具进行详细的讲述请求过程:
FTP地址格式:
无密码:
ftp://<服务器地址>
有密码:
ftp://<login>:<password>@<ftpserveraddress>
ftp采用两个TCP连接来传输一个文件,一个是控制连接,一个是数据连接,如下图所示:
通常Unix实现的ftp客户和服务器使用流方式传输
我们通过终端登录到FTP服务端以后会返回如下信息:
230 Login successful.
Remote system type is UNIX.
Using binary mode to transfer files.
下面我们开始具体的实现:
Downloading a directory listing via FTP is slightly different from downloading or uploading a file. This is because the incoming data has to be parsed. First, set up a read stream to get the directory listing. This should be done as it was for downloading a file: create the stream, register a callback function, schedule the stream with the run loop (if necessary, set up user name, password and proxy information), and finally open the stream. In the following example you do not need both a read and a write stream when retrieving the directory listing, because the incoming data is going to the screen rather than a file.
In the callback function, watch for the kCFStreamEventHasBytesAvailable
event.