FTP协议大家都知道是文件传输协议,那这个协议到底是怎么样的呢,怎么用C语言是实现一个客户端呢?
我想通过这篇文章你能够理解FTP协议。
1-首先我们先了解一下FTP的工作流程
FTP分为 FTP服务器,FTP客户端,一般我们只关注FTP客户端,这里也会只讲一下客户端。
1.1 命令端口连接
1.2 登录
1.3 数据端口连接(主动连接,被动连接)
主动连接:客户端打开一个数据端口给服务器连接
被动连接:服务器打开一个数据端口给客户端连接
1.4 命令交互和文件下载上传操作
一个FTP客户端一般要经历以上流程,其实1.1和1.3就是两个socket连接,一个用来传输命令,一个用来传输文件。
2-直接用工具演示
准备一个网络调试组手,一个可用的FTP服务器,网络调试组手什么样的都可以。
下面打开2个调试组手,左边的用来连接命令端口,右边用来连接数据端口
2.1 命令端口连接 ,用组手连接成功后,会返回 220 pyftpdlib 1.5.6 ready.
2.2 发送登录用户命令 USER user 返回 331 Username ok, send password.
发送登录密码命令 PASS 123456 返回 230 Login successful.
2.3 数据端口连接(因为我这个FTP服务器是公网的就不开放出来了)
使用被动模式连接 PASV 返回 227 Entering passive mode (104,214,126,31,181,148)
这里数据端口连接一定要快准狠,因为服务器很有超时等待。
括号里面就是数据端口要连接的 IP 和端口,IP: 104,214,126,31 ,端口:181*256+148
2.4 下载文件
下载命令:RETR test.txt ,返回:125 Data connection already open. Transfer starting.
226 Transfer complete.
发送下载命令后,服务器会把文件发送到数据端口中,发送完成数据端口会自动关闭。
2.5 断开连接
断开命令:QUIT 返回:221 Goodbye.
以上就是一个FTP客户端的连接-下载文件的一个过程,是不是很简单,自己用c语言写一个是不是也不难。
FTP的命令非常多,就不一样举例了。
下面列举一些常用的命令(命令后面都需要有 \r\n 就是回车)
命令 | 描述 | RFC | Description |
ABOR | (ABORT)此命令使服务器终止前一个FTP服务命令以及任何相关数据传输。 | Abort an active file transfer. | |
ACCT | (ACCOUNT)此命令的参数部分使用一个Telnet字符串来指明用户的账户。 | Account information. | |
ADAT | (AUTHENTICATION/SECURITY DATA)认证/安全数据 | RFC 2228 | Authentication/Security Data |
ALLO | 为接收一个文件分配足够的磁盘空间 | Allocate sufficient disk space to receive a file. | |
APPE | 增加 | Append. | |
AUTH | 认证/安全机制 | RFC 2228 | Authentication/Security Mechanism |
CCC | 清除命令通道 | RFC 2228 | Clear Command Channel |
CDUP | 改变到父目录 | Change to Parent Directory. | |
CONF | 机密性保护命令 | RFC 2228 | Confidentiality Protection Command |
CWD | 改变工作目录 | Change working directory. | |
DELE | 删除文件 | Delete file. | |
ENC | 隐私保护通道 | RFC 2228 | Privacy Protected Channel |
EPRT | 为服务器指定要连接的扩展地址和端口 | RFC 2428 | Specifies an extended address and port to which the server should connect. |
EPSV | 进入扩展被动模式 | RFC 2428 | Enter extended passive mode. |
FEAT | 获得服务器支持的特性列表 | RFC 2389 | Get the feature list implemented by the server. |
HELP | 如果指定了命令,返回命令使用文档;否则返回一个通用帮助文档 | Returns usage documentation on a command if specified, else a general help document is returned. | |
HOST | 确定服务器上所需的虚拟主机名。 | RFC 7151 | Identify desired virtual host on server, by name. |
LANG | 语言协商 | RFC 2640 | Language Negotiation |
LIST | 如果指定了文件或目录,返回其信息;否则返回当前工作目录的信息 | Returns information of a file or directory if specified, else information of the current working directory is returned. | |
LPRT | 为服务器指定要连接的长地址和端口 | RFC 1639 | Specifies a long address and port to which the server should connect. |
LPSV | 进入长被动模式 | RFC 1639 | Enter long passive mode. |
MDTM | 返回指定文件的最后修改时间 | RFC 3659 | Return the last-modified time of a specified file. |
MIC | 完整性保护命令 | RFC 2228 | Integrity Protected Command |
MKD | 创建目录 | Make directory. | |
MLSD | 如果目录被命名,列出目录的内容 | RFC 3659 | Lists the contents of a directory if a directory is named. |
MLST | 提供命令行指定的对象的数据 | RFC 3659 | Provides data about exactly the object named on its command line, and no others. |
MODE | 设定传输模式(流、块或压缩,S=流模式,B=块模式,C=压缩模式) | Sets the transfer mode (Stream, Block, or Compressed). | |
NLST | 返回指定目录的文件名列表 | Returns a list of file names in a specified directory. | |
NOOP | 无操作(哑包;通常用来保活) | No operation (dummy packet; used mostly on keepalives). | |
OPTS | 为特性选择选项 | RFC 2389 | Select options for a feature. |
PASS | 认证密码 | Authentication password. | |
PASV | 进入被动模式 | Enter passive mode. | |
PBSZ | 保护缓冲大小 | RFC 2228 | Protection Buffer Size |
PORT | 指定服务器要连接的地址和端口 | Specifies an address and port to which the server should connect. | |
PROT | 数据通道保护级别 | RFC 2228 | Data Channel Protection Level. |
PWD | 打印工作目录,返回主机的当前目录 | Print working directory. Returns the current directory of the host. | |
QUIT | 断开连接 | Disconnect. | |
REIN | 重新初始化连接 | Re initializes the connection. | |
REST | 从指定点重新开始传输 | Restart transfer from the specified point. | |
RETR | 传输文件副本 | Retrieve a copy of the file | |
RMD | 删除目录 | Remove a directory. | |
RNFR | 从...重命名 | Rename from. | |
RNTO | 重命名到... | Rename to. | |
SITE | 发送站点特殊命令到远端服务器 | Sends site specific commands to remote server. | |
SIZE | 返回文件大小 | RFC 3659 | Return the size of a file. |
SMNT | 挂载文件结构 | Mount file structure. | |
STAT | 返回当前状态 | Returns the current status. | |
STOR | 接收数据并且在服务器站点保存为文件 | Accept the data and to store the data as a file at the server site | |
STOU | 唯一地保存文件 | Store file uniquely. | |
STRU | 设定文件传输结构 | Set file transfer structure. | |
SYST | 返回系统类型 | Return system type. | |
TYPE | 设定传输模式(ASCII/二进制) A/I. | Sets the transfer mode (ASCII/Binary). | |
USER | 认证用户名 | Authentication username. | |
XCUP | 改变之当前工作目录的父目录 | RFC 775 | Change to the parent of the current working directory |
XMKD | 创建目录 | RFC 775 | Make a directory |
XPWD | 打印当前工作目录 | RFC 775 | Print the current working directory |
XRCP | RFC 743 | ||
XRMD | 删除目录 | RFC 775 | Remove the directory |
XRSQ | RFC 743 | ||
XSEM | 发送,否则邮件 | RFC 737 | Send, mail if cannot |
XSEN | 发送到终端 | RFC 737 | Send to terminal |
响应代码 | 解释说明 |
110 | 新文件指示器上的重启标记 |
120 | 服务器准备就绪的时间(分钟数) |
125 | 打开数据连接,开始传输 |
150 | 打开连接 |
200 | 成功 |
202 | 命令没有执行 |
211 | 系统状态回复 |
212 | 目录状态回复 |
213 | 文件状态回复 |
214 | 帮助信息回复 |
215 | 系统类型回复 |
220 | 服务就绪 |
221 | 退出网络 |
225 | 打开数据连接 |
226 | 结束数据连接 |
227 | 进入被动模式(IP 地址、ID 端口) |
230 | 登录因特网 |
250 | 文件行为完成 |
257 | 路径名建立 |
331 | 要求密码 |
332 | 要求帐号 |
350 | 文件行为暂停 |
421 | 服务关闭 |
425 | 无法打开数据连接 |
426 | 结束连接 |
450 | 文件不可用 |
451 | 遇到本地错误 |
452 | 磁盘空间不足 |
500 | 无效命令 |
501 | 错误参数 |
502 | 命令没有执行 |
503 | 错误指令序列 |
504 | 无效命令参数 |
530 | 未登录网络 |
532 | 存储文件需要帐号 |
550 | 文件不可用 |
551 | 不知道的页类型 |
552 | 超过存储分配 |
553 | 文件名不允许 |