CFtpServer::OnCommand Method  
 
virtual void OnCommand(
  UINT nClientId,  
  LPCTSTR lpszCommand  
);

A virtual method that is invoked after the client has sent a command to the server.

Parameters

nClientId
An unsigned integer which uniquely identifies the client session.
lpszCommand
A pointer to a string that specifies the command issued by the client. The command name will always be capitalized. For a complete list of commands supported by the server, see Server Commands.

Return Value

None.

Remarks

The OnCommand event handler is invoked after the client has sent a command to the server, but before the command has been processed. To implement an event handler, the application should create a class derived from the CFtpServer class, and then override this method.

This event handler is invoked for all commands issued by the client, including invalid or disabled commands. If the event handler processes the command, it must call the SendResponse method to send a success or error response back to the client. If this is not done, the server will perform the default processing for the command.

Although this event handler will provide the command name, the full command line can be obtained by using the GetCommandLine method. Individual command parameters can be obtained by using the GetCommandParam and GetCommandParamCount methods.

It is not necessary to use this event handler to disable a command. The EnableCommand method can be used to enable or disable specific commands, and the IsCommandEnabled method can be used to determine if a command is enabled.

If this event handler is used to implement a custom command, it is recommended that you use the IsClientAuthenticated method to determine whether or not the client session has been authenticated. Unless there is a specific need for the custom command to be used before a client has logged in, the application should not take any action and send a 530 result code back to the client indicating authentication is required.

Example

VOID CMyFtpServer::OnCommand(UINT nClientId, LPCTSTR lpszCommand)
{
    // Implement a custom command named TIME that will return the local time
    if (lstrcmp(lpszCommand, _T("TIME")) == 0)
    {
        // The command should not have any parameters
        if (GetCommandParamCount(nClientId) > 0)
        {
            SendResponse(nClientId, FTP_REPLY_BADARG);
            return;
        }

        if (IsClientAuthenticated(nClientId))
        {
            CString strTime = CTime::GetCurrentTime().Format(_T("%Y-%m-%d %H:%M:%S"));
            SendResponse(nClientId, FTP_REPLY_CMDOK, strTime);
        }
        else
        {
            // The client has not logged in, return an error
            SendResponse(nClientId, FTP_REPLY_NOLOGIN);
        }
    }
}

Requirements

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

See Also

DisconnectClient, EnableCommand, IsCommandEnabled, OnDisconnect, SendResponse