FtpAsyncConnect Function  
 
HCLIENT WINAPI FtpAsyncConnect(
  LPCTSTR lpszRemoteHost,  
  UINT nRemotePort,  
  LPCTSTR lpszUserName,  
  LPCTSTR lpszPassword,  
  UINT nTimeout,  
  DWORD dwOptions,  
  LPSECURITYCREDENTIALS lpCredentials,  
  HWND hEventWnd,  
  UINT uEventMsg  
);

The FtpAsyncConnect function establishes a connection with the specified server.

This function has been deprecated and may be unavailable in future releases. It was designed for use in legacy single-threaded applications and requires the application to have a message pump to process event messages. It should not be used with applications which are designed to execute as a service or those which do not have a graphical user interface.

The application should create a background worker thread and establish a connection by calling FtpConnect within that thread. If the application requires multiple simultaneous connections, it is recommended you create a worker thread for each client session.

Parameters

lpszRemoteHost
The name of the server to connect to; this may be a fully-qualified domain name or an IP address.
nRemotePort
The port number the server is listening on. A value of zero specifies that the default port number should be used. For standard connections, the default port number is 21. For secure connections, the default port number is 990. If the secure port number is specified, an implicit TLS connection will be established by default.
lpszUserName
Points to a string that specifies the user name to be used to authenticate the current client session. If this parameter is NULL or an empty string, then the login is considered to be anonymous. Note that anonymous logins are not supported for secure connections using the SSH protocol.
lpszPassword
Points to a string that specifies the password to be used to authenticate the current client session. This parameter may be NULL or an empty string if no password is required for the specified user, or if no username has been specified.
nTimeout
The number of seconds that the client will wait for a response from the server before failing the current operation.
dwOptions
An unsigned integer that specifies one or more options. This parameter is constructed by using a bitwise operator with any of the following values:
Value Description
FTP_OPTION_DEFAULT Default options should be used for file transfers. This is the same as specifying that all file transfers should use passive mode when establishing a data connection with the server. It is recommended most applications use passive mode to prevent potential compatibility issues with certain types of firewalls and routers which use Network Address Translation (NAT).
FTP_OPTION_PASSIVE This option specifies file transfers should attempt to establish the data connection with the server. If the local system is behind a firewall or a NAT router, the server may not be able to establish a data connection back to the client and the transfer will fail. This option forces the client to establish an outbound data connection with the server. It is recommended that applications use passive mode whenever possible.
FTP_OPTION_FIREWALL This option specifies the client should always use the host IP address to establish the data connection with the server, not the address returned by the server in response to the PASV command. This option may be necessary if the server is behind a router that performs Network Address Translation (NAT) and it returns an unreachable IP address for the data connection. If this option is specified, it will also enable passive mode data transfers.
FTP_OPTION_NOAUTH This option specifies the server does not require authentication, or that it requires an alternate authentication method. When this option is used, the client connection is flagged as authenticated as soon as the connection to the server has been established. Note that using this option to bypass authentication may result in subsequent errors when attempting to retrieve a directory listing or transfer a file. It is recommended that you consult the technical reference documentation for the server to determine its specific authentication requirements.
FTP_OPTION_KEEPALIVE This option specifies the client should attempt to keep the connection with the server active for an extended period of time. It is important to note that regardless of this option, the server may still choose to disconnect client sessions that are holding the command channel open but are not performing file transfers.
FTP_OPTION_VIRTUALHOST This option specifies the server supports virtual hosting, where multiple domains are hosted by a server using the same external IP address. If this option is enabled, the client will send the HOST command to the server upon establishing a connection.
FTP_OPTION_VERIFY This option specifies that file transfers should be automatically verified after the transfer has completed. If the server supports the XMD5 command, the transfer will be verified by calculating an MD5 hash of the file contents. If the server does not support the XMD5 command, but does support the XCRC command, the transfer will be verified by calculating a CRC32 checksum of the file contents. If neither the XMD5 or XCRC commands are supported, the transfer is verified by comparing the size of the file. Automatic file verification is only performed for binary mode transfers because of the end-of-line conversion that may occur when text files are uploaded or downloaded.
FTP_OPTION_TRUSTEDSITE This option specifies the server is trusted. The server certificate will not be validated and the connection will always be permitted. This option only affects connections using the TLS protocol.
FTP_OPTION_SECURE This option specifies the client should attempt to establish a secure connection with the server. This option is the same as specifying FTP_OPTION_SECURE_IMPLICIT which immediately performs the TLS protocol negotiation when the connection is established.
FTP_OPTION_SECURE_IMPLICIT This option specifies the client should attempt to immediately establish secure TLS connection with the server. This option is typically used when connecting to a server on port 990, which is the default port number used for FTPS.
FTP_OPTION_SECURE_EXPLICIT This option specifies the client should establish a standard connection to the server and then use the AUTH command to negotiate an explicit secure connection. This option is typically used when connecting to the server on ports other than 990.
FTP_OPTION_SECURE_SHELL This option specifies the client should use the Secure Shell (SSH) protocol to establish the connection. This option will automatically be selected if the connection is established using port 22, the default port for SSH.
FTP_OPTION_SECURE_FALLBACK This option specifies the client should permit the use of less secure cipher suites for compatibility with legacy servers. If this option is specified, the client will allow connections using TLS 1.0 and cipher suites that use RC4, MD5 and SHA1.
FTP_OPTION_TUNNEL This option specifies that a tunneled TCP connection and/or port-forwarding is being used to establish the connection to the server. This changes the behavior of the client with regards to internal checks of the destination IP address and remote port number, default capability selection and how the connection is established. This option also forces all connections to be outbound and enables the firewall compatibility features in the client.
FTP_OPTION_KEEPALIVE_DATA This option specifies the client should attempt to keep the control connection active during a file transfer. Normally, when a data transfer is in progress, no additional commands are issued on the control channel until the transfer completes. Specifying this option automatically enables the FTP_OPTION_KEEPALIVE option and forces the client to continue to issue NOOP commands during the file transfer. This option only applies to FTP and FTPS connections and has no effect on connections using SFTP (SSH).
FTP_OPTION_PREFER_IPV6 This option specifies the client should prefer the use of IPv6 if the server hostname can be resolved to both an IPv6 and IPv4 address. This option is ignored if the local system does not have IPv6 enabled, or when the hostname can only be resolved to an IPv4 address. If the server hostname can only be resolved to an IPv6 address, the client will attempt to establish a connection using IPv6 regardless if this option has been specified.
FTP_OPTION_FREETHREAD This option specifies the handle returned by this function may be used by any thread, and is not limited to the thread which created it. The application is responsible for ensuring that access to the handle is synchronized across multiple threads.
FTP_OPTION_HIRES_TIMER This option specifies the elapsed time for data transfers should be returned in milliseconds instead of seconds. This will return more accurate transfer times for smaller files being uploaded or downloaded using fast network connections.
FTP_OPTION_TLS_REUSE This option specifies that TLS session reuse should be enabled when establishing a secure FTP connection. This option is only supported on Windows 8.1 or Windows Server 2012 R2 and later platforms, and should only be enabled when explicitly required by the server. This option can be used with TLS 1.2 and TLS 1.3, although the mechanism used for session resumption differs between protocol versions.
lpCredentials
A pointer to a SECURITYCREDENTIALS structure. This parameter should be NULL if the connection is not secure or when client credentials are not required. Most servers do not require a client certificate to establish a secure connection. However, if the server does require a client certificate, the structure members dwSize, lpszCertStore and lpszCertName must be defined. Undefined structure members must be initialized to a value of zero or NULL and the dwSize member must be initialized to the size of the SECURITYCREDENTIALS structure.
hEventWnd
The handle to the event notification window. This window receives messages which notify the client of various asynchronous network events that occur.
uEventMsg
The message identifier that is used when an asynchronous network event occurs. This value should be greater than WM_USER as defined in the Windows header files.

Return Value

If the function succeeds, the return value is a handle to a client session. If the function fails, the return value is INVALID_CLIENT. To get extended error information, call FtpGetLastError.

Remarks

When a message is posted to the notification window, the low word of the lParam parameter contains the event identifier. The high word of lParam contains the low word of the error code, if an error has occurred. The wParam parameter contains the client handle. One or more of the following event identifiers may be sent:

Value Description
FTP_EVENT_CONNECT The control connection to the server has completed. The high word of the lParam parameter should be checked, since this notification message will be posted if an error has occurred.
FTP_EVENT_DISCONNECT The server has closed the control connection to the client. The client should read any remaining data and disconnect.
FTP_EVENT_OPENFILE The data connection to the server has completed. The high word of the lParam parameter should be checked, since this notification message will be posted if an error has occurred.
FTP_EVENT_CLOSEFILE The server has closed the data connection to the client. The client should read any remaining data and close the data channel.
FTP_EVENT_READFILE Data is available to read by the calling process. No additional messages will be posted until the client has read at least some of the data. This event is only generated if the client is in asynchronous mode.
FTP_EVENT_WRITEFILE The client can now write data. This notification is sent after a connection has been established, or after a previous attempt to write data has failed because it would result in a blocking operation. This event is only generated if the client is in asynchronous mode.
FTP_EVENT_TIMEOUT The network operation has exceeded the specified timeout period. The client application may attempt to retry the operation, or may disconnect from the server and report an error to the user.
FTP_EVENT_CANCEL The current operation has been canceled. Under most circumstances the client should disconnect from the server and re-connect if needed. After an operation has been canceled, the server may abort the connection or refuse to accept further commands from the client.
FTP_EVENT_COMMAND A command has been issued by the client and the server response has been received and processed. This event can be used to log the result codes and messages returned by the server in response to actions taken by the client.
FTP_EVENT_PROGRESS The client is in the process of sending or receiving a file on the data channel. This event is called periodically during a transfer so that the client can update any user interface components such as a status control or progress bar.
FTP_EVENT_GETFILE This event occurs when a file download has completed. If multiple files are being downloaded, this event will be generated for each file.
FTP_EVENT_PUTFILE This event occurs when a file upload has completed. If multiple files are being uploaded, this event will be generated for each file.

To cancel asynchronous notification and return the client to a blocking mode, use the FtpDisableEvents function.

If the FTP_OPTION_KEEPALIVE option is specified, a background worker thread will be created to monitor the command channel and periodically send NOOP commands to the server if no commands have been sent recently. This can prevent the server from terminating the client connection during idle periods where no commands are being issued. However, it is important to keep in mind that many servers can be configured to also limit the total amount of time a client can be connected to the server, as well as the amount of time permitted between file transfers. If the server does not respond to the NOOP command, this option will be automatically disabled for the remainder of the client session.

If the FTP_OPTION_SECURE_EXPLICIT option is specified, the client will establish a standard connection to the server and send the AUTH TLS command to the server. If the server does not accept this command, it will then send the AUTH SSL command. If both commands are rejected by the server, an explicit TLS session cannot be established. By default, both the command and data channels will be encrypted when a secure connection is established. To change this, use the FtpSetChannelMode function.

The FTP_OPTION_TLS_REUSE option allows a client and server to reuse security information from an existing TLS connection when establishing another secure connection. This can reduce the time and processing required to establish the new connection. For secure FTP (FTPS), session reuse is particularly important because the protocol uses separate connections for commands and file transfers. Some FTP servers require the data connection to reuse the security context established by the control connection as an additional security measure.

The mechanism used for TLS session resumption depends on the protocol version. TLS 1.2 and earlier versions use traditional session identifiers or session tickets, while TLS 1.3 uses a different mechanism based on pre-shared keys and session tickets. When TLS session reuse is enabled, the library uses the same security context for both the FTP control and data connections, regardless of the negotiated TLS version. The actual resumption behavior depends on the TLS implementation and server support.

The FTP_OPTION_FREETHREAD option enables any thread to call any function using the handle, regardless of which thread created it. It is important to note that this option disables certain internal safety checks which are performed by the library and may result in unexpected behavior unless access to the handle is synchronized. If one thread calls a function in the library, it must ensure that no other thread will call another function at the same time using the same handle. By default, the handle is initially attached to the thread that created it. From that point on, until the handle is released, only the owner may call functions using that handle. The ownership of the handle may be transferred from one thread to another using the FtpAttachThread function.

Example

// The Ipswitch WS_FTP server accepts the AUTH command to establish
// an explicit TLS session on the default FTP port     
hClient = FtpAsyncConnect(lpszRemoteHost,
                          FTP_PORT_DEFAULT,
                          FTP_TIMEOUT,
                          FTP_OPTION_SECURE_EXPLICIT,
                          NULL,
                          hEventWnd,
                          uEventMsg);      

// When the GlobalSCAPE Secure FTP server is configured in implicit
// authorization mode, it negotiates a secure session as soon as the
// connection is established and does not require a command      
hClient = FtpAsyncConnect(lpszRemoteHost,
                          FTP_PORT_SECURE,
                          FTP_TIMEOUT,
                          FTP_OPTION_SECURE_IMPLICIT,
                          NULL,
                          hEventWnd,
                          uEventMsg);
      

Requirements

Minimum Desktop Platform: Windows 7 Service Pack 1
Minimum Server Platform: Windows Server 2008 R2 Service Pack 1
Header File: cstools12.h
Import Library: csftpv12.lib
Unicode: Implemented as Unicode and ANSI versions

See Also