FTP 数据连接重用

FTP data connections reuse

我正在开发一个 FTP 客户端,我正在尝试了解数据连接的工作流程。

据我了解,初始 (command) 连接在您退出之前是永久性的。但是,我不确定数据连接 - 它是否按命令重新启动?所以你调用 PORT ...PASV,获得第二个连接,执行 LIST,得到结果,连接关闭,重新开始?

另外,每次连接关闭后是否需要再次调用PASV(或PORT ...)?似乎当我尝试使用被动连接测试某些东西时,在第一个命令返回结果并关闭数据连接后,我无法重新连接到同一端口。我可以继续调用 PASV -> 数据连接 -> 运行 命令 -> 获取结果 -> 数据连接关闭 -> PASV,但它似乎不是 运行?

此外,如果有人在 FTP 上有比 RFC 更简洁的好 material,我真的很感激。

您每次都必须打开一个新连接。这只是连接的关闭,您(或服务器)如​​何知道传输已完成(至少在常见的“流模式”下)。

您甚至不能重复使用 local/remote 端口号组合,因为当 TCP 连接关闭时,它会进入 TIME_WAIT 模式并且端口号组合在一段时间内无法使用。因此,对于两个立即连续的传输,您无论如何都必须使用不同的端口号组合。

参考RFC 959,第3.3节。数据管理:

Reuse of the Data Connection: When using the stream mode of data transfer the end of the file must be indicated by closing the connection. This causes a problem if multiple files are to be transfered in the session, due to need for TCP to hold the connection record for a time out period to guarantee the reliable communication. Thus the connection can not be reopened at once.

There are two solutions to this problem. The first is to negotiate a non-default port. The second is to use another transfer mode.

A comment on transfer modes. The stream transfer mode is inherently unreliable, since one can not determine if the connection closed prematurely or not. The other transfer modes (Block, Compressed) do not close the connection to indicate the end of file. They have enough FTP encoding that the data connection can be parsed to determine the end of the file. Thus using these modes one can leave the data connection open for multiple file transfers.


另请参阅: