I use WinInet to connect to an FTP server. I use FtpCommand()
to send a "PASV" command to switch from Active to Passive mode. I am now searching for the opposite command to switch from Passive to Active mode. Does anyone know how to do this?
Active mode is enabled by sending a PORT
(or EPRT
) command instead of sending a PASV
(or EPSV
) command. PORT
/EPRT
tells the server which IP/port it needs to actively connect to on your system.
If you want to understand how the FTP protocol works, I suggest you read the FTP specification, RFC 959, and its various extensions, particularly RFC 2428 and RFC 3659.
In WinInet, the transfer mode is typically established at the beginning of the session when you call 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()
functions to operate over their own data connections. Once the mode is established for a session, it cannot be changed, AFAIK.
However, you can use 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()
to transfer files and directory listings over that data connection.