WinInet FTP 连接时将被动模式切换为主动模式?

WinInet FTP switch Passive to Active mode while connected?

我使用 WinInet 连接到 FTP 服务器。我使用 FtpCommand() 发送 "PASV" 命令从主动模式切换到被动模式。我现在正在寻找相反的命令以从被动模式切换到主动模式。有人知道怎么做吗?

通过发送 PORT (or EPRT) command instead of sending a PASV (or EPSV) 命令启用活动模式。 PORT/EPRT 告诉服务器 IP/port 它需要在您的系统上主动连接到哪个服务器。

如果你想了解FTP协议是如何工作的,我建议你阅读FTP规范,RFC 959, and its various extensions, particularly RFC 2428 and RFC 3659

在 WinInet 中,传输模式通常是在会话开始时调用 InternetConnect() or InternetOpenUrl(). If you specify the INTERNET_FLAG_PASSIVE flag, it forces Passive mode. If you do not specify the flag, the mode is determined by the user's default Internet Options. This mode allows the FtpGetFile()/FtpPutFile() and FtpFindFirstFile()/InternetFindNextFile() 函数以在其自己的数据连接上进行操作时建立的。一旦为会话建立模式,就无法更改,AFAIK。

但是,您可以使用 FtpCommand() to send any FTP command manually, including PASV/EPSV and PORT/EPRT. If you set the fExpectResponse parameter to TRUE, the phFtpCommand output parameter will give you a new HINTERNET handle if a data socket is created. You can use that handle with InternetReadFile() and InternetWriteFile() 通过该数据连接传输文件和目录列表。