Table of Contents

Class FtpSocketStream

Namespace
FluentFTP
Assembly
FluentFTP.dll

Stream class used for talking. Used by FtpClient, extended by FtpDataStream

public class FtpSocketStream : Stream, IDisposable, IAsyncDisposable
Inheritance
FtpSocketStream
Implements
Derived
Inherited Members
Extension Methods

Constructors

FtpSocketStream(BaseFtpClient)

This stream connection to be associated with this client

public FtpSocketStream(BaseFtpClient conn)

Parameters

conn BaseFtpClient

Fields

Client

The client this stream is associated with

public readonly BaseFtpClient Client

Field Value

BaseFtpClient

SslSessionLength

Keep a count of command/response transactions on the control connection

public int SslSessionLength

Field Value

int

Properties

BaseStream

Gets the underlying stream, could be a NetworkStream, SslStream or CustomStream

protected Stream BaseStream { get; }

Property Value

Stream

CanRead

Gets a value indicating if this stream can be read

public override bool CanRead { get; }

Property Value

bool

CanSeek

Gets a value indicating if this stream if seekable

public override bool CanSeek { get; }

Property Value

bool

CanWrite

Gets a value indicating if this stream can be written to

public override bool CanWrite { get; }

Property Value

bool

ConnectTimeout

Gets or sets the length of time milliseconds to wait for a connection succeed before giving up. The default is 0 = disable, use system default timeout.

public int ConnectTimeout { get; set; }

Property Value

int

IsConnected

Gets a value indicating if this socket stream is connected

public bool IsConnected { get; }

Property Value

bool

IsControlConnection

Is this stream the control connection?

public bool IsControlConnection { get; set; }

Property Value

bool

IsDisposed

Gets a value indicating if this socket stream is disposed

public bool IsDisposed { get; set; }

Property Value

bool

IsEncrypted

Gets a value indicating if encryption is being used

public bool IsEncrypted { get; }

Property Value

bool

Length

Gets the length of the stream

public override long Length { get; }

Property Value

long

LocalEndPoint

Gets the local end point of the socket

public IPEndPoint LocalEndPoint { get; }

Property Value

IPEndPoint

Position

Gets the current position of the stream. Trying to set this property throws an InvalidOperationException()

public override long Position { get; set; }

Property Value

long

ReadTimeout

Gets or sets the amount of time to wait for a read operation to complete. Default value is Timeout.Infinite.

public override int ReadTimeout { get; set; }

Property Value

int

RemoteEndPoint

Gets the remote end point of the socket

public IPEndPoint RemoteEndPoint { get; }

Property Value

IPEndPoint

Socket

The socket used for talking

protected Socket Socket { get; }

Property Value

Socket

SocketPollInterval

Gets or sets the length of time in milliseconds that must pass since the last socket activity before calling Poll() on the socket to test for connectivity. Setting this interval too low will have a negative impact on performance. Setting this interval to 0 disables Poll()'ing all together. The default value is 15 seconds.

public int SocketPollInterval { get; set; }

Property Value

int

SslProtocolActive

The negotiated SSL/TLS protocol version. Will have a valid value after connection is complete.

public SslProtocols SslProtocolActive { get; }

Property Value

SslProtocols

Methods

Accept()

Accepts a connection from a listening socket

public void Accept()

AcceptAsync(CancellationToken)

Accepts a connection from a listening socket

public Task AcceptAsync(CancellationToken token = default)

Parameters

token CancellationToken

Returns

Task

ActivateEncryption(string, X509CertificateCollection, SslProtocols)

Activates SSL on this stream using the specified protocols. Fires the ValidateCertificate event. If this event is not handled and there are SslPolicyErrors present, the certificate will not be accepted.

public void ActivateEncryption(string targetHost, X509CertificateCollection clientCerts, SslProtocols sslProtocols)

Parameters

targetHost string

The host to authenticate the certificate against

clientCerts X509CertificateCollection

A collection of client certificates to use when authenticating the SSL stream

sslProtocols SslProtocols

A bitwise parameter for supported encryption protocols.

Exceptions

AuthenticationException

Thrown when authentication fails

ActivateEncryptionAsync(string, X509CertificateCollection, SslProtocols, CancellationToken)

Activates SSL on this stream using the specified protocols. Fires the ValidateCertificate event. If this event is not handled and there are SslPolicyErrors present, the certificate will not be accepted.

public Task ActivateEncryptionAsync(string targetHost, X509CertificateCollection clientCerts, SslProtocols sslProtocols, CancellationToken token = default)

Parameters

targetHost string

The host to authenticate the certificate against

clientCerts X509CertificateCollection

A collection of client certificates to use when authenticating the SSL stream

sslProtocols SslProtocols

A bitwise parameter for supported encryption protocols.

token CancellationToken

The token that can be used to cancel the entire process

Returns

Task

Exceptions

AuthenticationException

Thrown when authentication fails

Close()

Disconnects from server

public override void Close()

CloseAsync(CancellationToken)

CloseAsync, disconnects from server

public virtual ValueTask CloseAsync(CancellationToken token = default)

Parameters

token CancellationToken

Returns

ValueTask

Connect(string, int, FtpIpVersion)

Connect to the specified host

public void Connect(string host, int port, FtpIpVersion ipVersions)

Parameters

host string

The host to connect to

port int

The port to connect to

ipVersions FtpIpVersion

Internet Protocol versions to support during the connection phase

ConnectAsync(string, int, FtpIpVersion, CancellationToken)

Connect to the specified host

public Task ConnectAsync(string host, int port, FtpIpVersion ipVersions, CancellationToken token)

Parameters

host string

The host to connect to

port int

The port to connect to

ipVersions FtpIpVersion

Internet Protocol versions to support during the connection phase

token CancellationToken

The token that can be used to cancel the entire process

Returns

Task

Dispose()

Disconnects from server

protected void Dispose()

DisposeAsync()

DisposeAsync, disconnects from server

public override ValueTask DisposeAsync()

Returns

ValueTask

DisposeAsyncCore()

Disconnects from server, actually

protected ValueTask DisposeAsyncCore()

Returns

ValueTask

Flush()

Flushes the stream

public override void Flush()

FlushAsync(CancellationToken)

Flushes the stream asynchronously

public override Task FlushAsync(CancellationToken token)

Parameters

token CancellationToken

The CancellationToken for this task

Returns

Task

Listen(IPAddress, int)

Instructs this stream to listen for connections on the specified address and port

public void Listen(IPAddress address, int port)

Parameters

address IPAddress

The address to listen on

port int

The port to listen on

OnValidateCertificate(X509Certificate, X509Chain, SslPolicyErrors)

Fires the SSL certificate validation event

protected bool OnValidateCertificate(X509Certificate certificate, X509Chain chain, SslPolicyErrors errors)

Parameters

certificate X509Certificate

Certificate being validated

chain X509Chain

Certificate chain

errors SslPolicyErrors

Policy errors if any

Returns

bool

True if it was accepted, false otherwise

OnValidateCertificate(X509Certificate, X509Chain, string)

Fires the SSL certificate validation event for custom encrypted streams

protected bool OnValidateCertificate(X509Certificate certificate, X509Chain chain, string errorMessage)

Parameters

certificate X509Certificate

Certificate being validated

chain X509Chain

Certificate chain

errorMessage string

Policy errors if any

Returns

bool

True if it was accepted, false otherwise

Read(byte[], int, int)

Reads data from the stream

public override int Read(byte[] buffer, int offset, int count)

Parameters

buffer byte[]

Buffer to read into

offset int

Where in the buffer to start

count int

Number of bytes to be read

Returns

int

The amount of bytes read from the stream

Read(Span<byte>)

Reads data from the stream

public override int Read(Span<byte> buffer)

Parameters

buffer Span<byte>

Buffer to read into

Returns

int

The amount of bytes read from the stream

ReadAllLines(Encoding, int)

Reads all lines from the socket

public IEnumerable<string> ReadAllLines(Encoding encoding, int bufferSize)

Parameters

encoding Encoding

The type of encoding used to convert from byte[] to string

bufferSize int

The size of the buffer

Returns

IEnumerable<string>

A list of lines from the stream

ReadAllLinesAsync(Encoding, int, CancellationToken)

Reads all lines from the socket

public Task<IEnumerable<string>> ReadAllLinesAsync(Encoding encoding, int bufferSize, CancellationToken token)

Parameters

encoding Encoding

The type of encoding used to convert from byte[] to string

bufferSize int

The size of the buffer

token CancellationToken

The token that can be used to cancel the entire process

Returns

Task<IEnumerable<string>>

A list of lines from the stream

ReadAsync(byte[], int, int, CancellationToken)

Reads data from the stream

public override Task<int> ReadAsync(byte[] buffer, int offset, int count, CancellationToken token)

Parameters

buffer byte[]

Buffer to read into

offset int

Where in the buffer to start

count int

Number of bytes to be read

token CancellationToken

The CancellationToken for this task

Returns

Task<int>

The amount of bytes read from the stream

ReadAsync(Memory<byte>, CancellationToken)

Reads data from the stream

public override ValueTask<int> ReadAsync(Memory<byte> buffer, CancellationToken token)

Parameters

buffer Memory<byte>

Buffer to read into

token CancellationToken

The CancellationToken for this task

Returns

ValueTask<int>

The amount of bytes read from the stream

ReadLine(Encoding)

Reads a line from the socket

public string ReadLine(Encoding encoding)

Parameters

encoding Encoding

The type of encoding used to convert from byte[] to string

Returns

string

A line from the stream, null if there is nothing to read

ReadLineAsync(Encoding, CancellationToken)

Reads a line from the socket asynchronously

public Task<string> ReadLineAsync(Encoding encoding, CancellationToken token)

Parameters

encoding Encoding

The type of encoding used to convert from byte[] to string

token CancellationToken

The CancellationToken for this task

Returns

Task<string>

A line from the stream, null if there is nothing to read

Seek(long, SeekOrigin)

Throws an InvalidOperationException

public override long Seek(long offset, SeekOrigin origin)

Parameters

offset long

Ignored

origin SeekOrigin

Ignored

Returns

long

SetLength(long)

Throws an InvalidOperationException

public override void SetLength(long value)

Parameters

value long

Ignored

SetSocketOption(SocketOptionLevel, SocketOptionName, bool)

Sets socket options on the underlying socket

public void SetSocketOption(SocketOptionLevel level, SocketOptionName name, bool value)

Parameters

level SocketOptionLevel

SocketOptionLevel

name SocketOptionName

SocketOptionName

value bool

SocketOptionValue

Write(byte[], int, int)

Writes data to the stream

public override void Write(byte[] buffer, int offset, int count)

Parameters

buffer byte[]

Buffer to write to stream

offset int

Where in the buffer to start

count int

Number of bytes to be written

Write(ReadOnlySpan<byte>)

Writes data to the stream

public override void Write(ReadOnlySpan<byte> buffer)

Parameters

buffer ReadOnlySpan<byte>

Buffer to write to stream

WriteAsync(byte[], int, int, CancellationToken)

Writes data to the stream asynchronously

public override Task WriteAsync(byte[] buffer, int offset, int count, CancellationToken token)

Parameters

buffer byte[]

Buffer to write to stream

offset int

Where in the buffer to start

count int

Number of bytes to be written

token CancellationToken

The CancellationToken for this task

Returns

Task

WriteAsync(ReadOnlyMemory<byte>, CancellationToken)

Writes data to the stream asynchronously

public override ValueTask WriteAsync(ReadOnlyMemory<byte> buffer, CancellationToken token)

Parameters

buffer ReadOnlyMemory<byte>

Buffer to write to stream

token CancellationToken

The CancellationToken for this task

Returns

ValueTask

WriteLine(Encoding, string)

Writes a line to the stream using the specified encoding

public void WriteLine(Encoding encoding, string buf)

Parameters

encoding Encoding

Encoding used for writing the line

buf string

The data to write

WriteLineAsync(Encoding, string, CancellationToken)

Writes a line to the stream using the specified encoding asynchronously

public Task WriteLineAsync(Encoding encoding, string buf, CancellationToken token)

Parameters

encoding Encoding

Encoding used for writing the line

buf string

The data to write

token CancellationToken

The CancellationToken for this task

Returns

Task

Events

ValidateCertificate

Event is fired when a SSL certificate needs to be validated

public event FtpSocketStreamSslValidation ValidateCertificate

Event Type

FtpSocketStreamSslValidation