How can I access the underlying Socket of the Ftp client?

0 votes
asked Sep 6, 2016 by Rebex KB (8,210 points)
edited Sep 6, 2016 by Rebex KB

Is it possible to directly access the internal Socket object of the Rebex FTP/SSL client? I would like to fine-tune it.

Applies to: Rebex FTP/SSL

1 Answer

+1 vote
answered Sep 6, 2016 by Lukas Pokorny (88,510 points)
selected Sep 6, 2016 by Rebex KB
 
Best answer

Most of the options to fine-tune are already accessible through Ftp object's Proxy, Settings and PortRange properties. If you need to set any additional option, please let us know.

But if you absolutely need to achieve this right now yourself, you can use our socket factory functionality and implement a custom socket creation routine that provides a suitable socket to the Ftp object.

If you use .NET 4.0 or higher and don't need to connect through a proxy, the following approach can be used:

public class FtpExt : Ftp
{
    private class FtpSocketFactory : ISocketFactory
    {
        private readonly FtpExt _owner;

        public FtpSocketFactory(FtpExt owner)
        {
            _owner = owner;
        }

        ISocket ISocketFactory.CreateSocket()
        {
            var socket = new Socket(SocketType.Stream, ProtocolType.Tcp);

            var handler = _owner.SocketCreated;
            if (handler != null)
                handler(_owner, new SocketCreatedEventArgs(socket));

            return new ProxySocket(socket);
        }
    }

    public EventHandler<SocketCreatedEventArgs> SocketCreated;

    public FtpExt()
    {
        var factory = new FtpSocketFactory(this);
        SetSocketFactory(factory);
    }
}

The FtpExt object inherits from Ftp and adds a SocketCreated event that gets called whether a new socket has been created. Then, you can customize it, like this:

var ftp = new FtpExt();

ftp.SocketCreated += (sender, e) =>
{
    Socket socket = e.Socket;
    socket.SendBufferSize = 256 * 1024;
    socket.ReceiveBufferSize = 256 * 1024;
};

ftp.Connect("test.rebex.net");
ftp.Login("demo", "password");
ftp.GetList();
...