SFTPServer Component
Properties Methods Events Configuration Settings Errors
The SFTPServer component is used to create a SFTP Server.
Syntax
nsoftware.IPWorksSSH.Sftpserver
Remarks
The SFTPServer component provides a simple way to create a SFTP server. Any SFTP client will be able to connect and transfer files to and from the server.
To begin, first provide a valid certificate with private key in the SSHCert property.
Set the RootDirectory property to a valid local path. When clients connect they will see this as their initial directory.
To start the server set Listening to true.
When clients connect the SSHUserAuthRequest event is fired. This provides an opportunity to validate the client's credentials. Please see the help for SSHUserAuthRequest for more details.
This is all that is required to start the SFTP server and begin serving files.
During operation other events fire to provide further information and also to provide you an opportunity to override the default behavior and return errors if desired.
Threading in Server Applications
In .NET, all socket-based components perform all socket IO through asynchronous methods. Each call consumes a thread from the system's thread pool, and thus the number of concurrent calls is limited to the number of worker threads returned by System.Threading.ThreadPool.GetMaxThreads. When using several components that can each create a socket connection or a single component that can create multiple socket connections, the application may enter a state where all of the pool threads are in use for receiving. In a scenario such as this, the remote hosts may be waiting for data from the application before sending, but the application cannot send that data because all the threads are tied up waiting for inbound data. This is particularly important for server applications that use a daemon component.
For example, say a server application 'S' has a max thread pool size of three, and clients 'A' and 'B' connect to it and each client uploads a large file. After all uploads are complete, 'S' enqueues an asynchronous read request in the thread pool for each client. Since there is no work being done, the thread pool immediately consumes two threads which wait for any further data from 'A' and 'B'. While 'S' is still processing the first two files it has received, client 'C' connects and uploads a relatively small file. Similarly, once that upload has completed 'S' enqueues a third read request, and the thread pool immediately consumes the third and final thread to wait for further data from 'C'. Once 'S' completes processing any of the files it will enqueue a send request. However, since all three threads are in use and waiting for data from the clients, the send operation cannot be completed. The entire system becomes deadlocked because each of the clients is waiting for data from the server, which cannot send that data since all of its pool threads are in turn waiting for data from the clients.
To avoid a deadlock situation such as this, it is necessary to ensure that the maximum number of socket connections an application can make is always at least one less than the number of available worker threads. One method to achieve this is to call System.Threading.ThreadPool.SetMaxThreads during the application's startup to set the maximum number of threads to a value high enough to support the application for the duration of its execution. Another option is to trap an event such as ConnectionRequest and Disconnected from each component instance with logic to manage the max thread pool size according to the application's current needs.
Property List
The following is the full list of the properties of the component with short descriptions. Click on the links for further details.
ConnectionBacklog | The maximum number of pending connections maintained by the TCP/IP subsystem. |
Connections | A collection of currently connected SFTP clients. |
DefaultAuthMethods | Specifies the supported authentication methods. |
DefaultTimeout | An initial timeout value to be used by incoming connections. |
KeyboardInteractiveMessage | The instructions to send to the client during keyboard-interactive authentication. |
KeyboardInteractivePrompts | A collection of prompts to present to the user for keyboard-interactive authentication. |
Listening | If True, the component accepts incoming connections on LocalPort. |
LocalHost | The name of the local host or user-assigned IP interface through which connections are initiated or accepted. |
LocalPort | The TCP port in the local host where the component listens. |
RootDirectory | The root directory for the entire SFTP server. |
SSHCert | A certificate to be used during SSH negotiation. |
SSHCompressionAlgorithms | A comma-separated list containing all allowable compression algorithms. |
SSHEncryptionAlgorithms | A comma-separated list containing all allowable compression algorithms. |
Method List
The following is the full list of the methods of the component with short descriptions. Click on the links for further details.
Config | Sets or retrieves a configuration setting . |
Disconnect | Disconnect the specified client. |
DoEvents | Processes events from the internal message queue. |
ExchangeKeys | Causes the component to exchange a new set of session keys on the specified connection. |
SetFileList | Sets the file list for a connection during a directory listing request. |
Shutdown | Shuts down the server. |
Event List
The following is the full list of the events fired by the component with short descriptions. Click on the links for further details.
Connected | Fired immediately after a connection completes (or fails). |
ConnectionRequest | Fired when a request for connection comes from a remote host. |
DirCreate | Fires when a client wants to create a new directory. |
DirList | Fires when a client attempts to open a directory for listing. |
DirRemove | Fires when a client wants to delete a directory. |
Disconnected | Fired when a connection is closed. |
Error | Information about errors during data delivery. |
FileClose | Fires when a client attempts to close an open file or directory handle. |
FileOpen | Fires when a client wants to open or create a file. |
FileRead | Fires when a client wants to read from an open file. |
FileRemove | Fires when a client wants to delete a file. |
FileRename | Fires when a client wants to rename a file. |
FileWrite | Fires when a client wants to write to an open file. |
GetAttributes | Fires when a client needs to get file information. |
ResolvePath | Fires when a client attempts to canonicalize a path. |
SetAttributes | Fires when a client attempts to set file or directory attributes. |
SSHStatus | Shows the progress of the secure connection. |
SSHUserAuthRequest | Fires when a client attempts to authenticate a connection. |
Configuration Settings
The following is a list of configuration settings for the component with short descriptions. Click on the links for further details.
DirListBufferSize[ConnectionId] | The number of entries to be returned in one response to a request for a directory listing. |
ProtocolVersion | The highest allowable SFTP version to use. |
RestrictUserToHomeDir[ConnectionId] | Whether to restrict the user to their HomeDir. |
ServerEOL | Specifies the line endings used in files on the server. |
SFTPErrorMessage[ConnectionId] | Specifies the error message to be returned to the client. |
MaxAuthAttempts | The maximum authentication attempts allowed before forcing a disconnect. |
ServerSSHVersionString | The SSH version string sent to connecting clients. |
UserAuthBanner[ConnectionId] | A custom user authentication banner. |
KeyRenegotiationThreshold | Sets the threshold for the SSH Key Renegotiation. |
SSHKeyExchangeAlgorithms | Specifies the supported key exchange algorithms. |
SSHMacAlgorithms | Specifies the supported Mac algorithms. |
BindExclusively | Whether or not the component considers a local port reserved for exclusive use. |
InBufferSize | The size in bytes of the incoming queue of the socket. |
MaxConnections | The maximum number of connections available. |
OutBufferSize | The size in bytes of the outgoing queue of the socket. |
KeepAliveTime | The inactivity time in milliseconds before a TCP keep-alive packet is sent. |
KeepAliveInterval | The retry interval, in milliseconds, to be used when a TCP keep-alive packet is sent and no response is received. |
RecordLength[ConnectionId] | The length of received data records. |
TcpNoDelay | Whether or not to delay when sending packets. |
CodePage | The system code page used for Unicode to Multibyte translations. |