FTP协议使用

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 就是回车)

命令描述RFCDescription
ABOR(ABORT)此命令使服务器终止前一个FTP服务命令以及任何相关数据传输。 Abort an active file transfer.
ACCT(ACCOUNT)此命令的参数部分使用一个Telnet字符串来指明用户的账户。 Account information.
ADAT(AUTHENTICATION/SECURITY DATA)认证/安全数据RFC 2228Authentication/Security Data
ALLO为接收一个文件分配足够的磁盘空间 Allocate sufficient disk space to receive a file.
APPE增加 Append.
AUTH认证/安全机制RFC 2228Authentication/Security Mechanism
CCC清除命令通道RFC 2228Clear Command Channel
CDUP改变到父目录 Change to Parent Directory.
CONF机密性保护命令RFC 2228Confidentiality Protection Command
CWD改变工作目录 Change working directory.
DELE删除文件 Delete file.
ENC隐私保护通道RFC 2228Privacy Protected Channel
EPRT为服务器指定要连接的扩展地址和端口RFC 2428Specifies an extended address and port to which the server should connect.
EPSV进入扩展被动模式RFC 2428Enter extended passive mode.
FEAT获得服务器支持的特性列表RFC 2389Get 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 7151Identify desired virtual host on server, by name.
LANG语言协商RFC 2640Language Negotiation
LIST如果指定了文件或目录,返回其信息;否则返回当前工作目录的信息 Returns information of a file or directory if specified, else information of the current working directory is returned.
LPRT为服务器指定要连接的长地址和端口RFC 1639Specifies a long address and port to which the server should connect.
LPSV进入长被动模式RFC 1639Enter long passive mode.
MDTM返回指定文件的最后修改时间RFC 3659Return the last-modified time of a specified file.
MIC完整性保护命令RFC 2228Integrity Protected Command
MKD创建目录 Make directory.
MLSD如果目录被命名,列出目录的内容RFC 3659Lists the contents of a directory if a directory is named.
MLST提供命令行指定的对象的数据RFC 3659Provides 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 2389Select options for a feature.
PASS认证密码 Authentication password.
PASV进入被动模式 Enter passive mode.
PBSZ保护缓冲大小RFC 2228Protection Buffer Size
PORT指定服务器要连接的地址和端口 Specifies an address and port to which the server should connect.
PROT数据通道保护级别RFC 2228Data 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 3659Return 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 775Change to the parent of the current working directory
XMKD创建目录RFC 775Make a directory
XPWD打印当前工作目录RFC 775Print the current working directory
XRCP RFC 743 
XRMD删除目录RFC 775Remove the directory
XRSQ RFC 743 
XSEM发送,否则邮件RFC 737Send, mail if cannot
XSEN发送到终端RFC 737Send 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文件名不允许
  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值