' Collection of code snippets by Arne Vajhøj 
' (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) 
Imports System
Imports System.IO
Imports System.Text
Imports System.Net.Sockets
Imports System.Threading

Public Class FtpClient
    Private ctrl As TcpClient
    Private ctrlstm As NetworkStream
    Private data As TcpClient
    Private datastm As NetworkStream

    Private Shared Sub Send(ByVal stm As NetworkStream, ByVal line As String)
        Dim b As Byte() = Encoding.Default.GetBytes(line)
        stm.Write(b, 0, b.Length)
    End Sub

    Private Shared Function Receive(ByVal stm As NetworkStream) As String
        Dim b(100000 - 1) As Byte
        Dim ix As Integer = 0
        Dim n As Integer
        While stm.DataAvailable
            n = stm.Read(b, ix, b.Length - ix)
            ix += n
        End While
        Return Encoding.Default.GetString(b, 0, ix)
    End Function

    Public Shared Function Command(ByVal stm As NetworkStream, ByVal cmd As String) As String
        Send(stm, cmd + Convert.ToChar(13) + Convert.ToChar(10))
        Thread.Sleep(200)
        Return Receive(stm)
    End Function

    Private Sub SetupData()
        Dim dataaddr As String = Command(ctrlstm, "PASV")
        Dim addrparts As String() = dataaddr.Split("()".ToCharArray)(1).Split(",".ToCharArray)
        Dim datahost As String = addrparts(0) + "." + addrparts(1) + "." + addrparts(2) + "." + addrparts(3)
        Dim dataport As Integer = Integer.Parse(addrparts(4)) * 256 + Integer.Parse(addrparts(5))
        data = New TcpClient (datahost, dataport)
        datastm = data.GetStream
    End Sub

    Private Function SetupData1() As String
        Dim dataaddr As String = Command(ctrlstm, "PASV")
        Dim addrparts As String = dataaddr.Split("()".ToCharArray)(1)
        SetupData1 = addrparts
    End Function

    Private Sub SetupData2(ByVal addr As String)
        Command(ctrlstm, "PORT " + addr)
    End Sub

    Public Sub New(ByVal host As String, ByVal username As String, ByVal password As String)
        ctrl = New TcpClient (host, 21)
        ctrlstm = ctrl.GetStream
        Command(ctrlstm, "USER " + username)
        Command(ctrlstm, "PASS " + password)
    End Sub

    Public Function Dir() As String
        SetupData
        Command(ctrlstm, "LIST")
        Dim res As String = Receive(datastm)
        Receive(ctrlstm)
        datastm.Close
        data.Close
        Return res
    End Function

    Public Function ShortDir() As String
        SetupData
        Command(ctrlstm, "NLST")
        Dim res As String = Receive(datastm)
        Receive(ctrlstm)
        datastm.Close
        data.Close
        Return res
    End Function

    Public Sub MkDir(ByVal dir As String)
        Command(ctrlstm, "MKD " + dir)
    End Sub

    Public Sub RmDir(ByVal dir As String)
        Command(ctrlstm, "RMD " + dir)
    End Sub

    Public Sub ChDir(ByVal dir As String)
        Command(ctrlstm, "CWD " + dir)
    End Sub

    Public Sub UpLoad(ByVal filename As String, ByVal binary As Boolean)
        If binary Then
            Command(ctrlstm, "TYPE I")
        Else
            Command(ctrlstm, "TYPE A")
        End If
        SetupData
        Command(ctrlstm, "STOR " + filename)
        Receive(ctrlstm)
        Dim f As FileStream = New FileStream (filename, FileMode.Open)
        Dim b(100000 - 1) As Byte
        Dim ix As Integer = 0
        Dim n As Integer
        While ix < f.Length
            n = f.Read(b, 0, b.Length)
            datastm.Write(b, 0, n)
            Thread.Sleep(100)
            ix += n
        End While
        f.Close
        datastm.Close
        data.Close
    End Sub

    Public Sub DownLoad(ByVal filename As String, ByVal binary As Boolean)
        If binary Then
            Command(ctrlstm, "TYPE I")
        Else
            Command(ctrlstm, "TYPE A")
        End If
        SetupData
        Command(ctrlstm, "RETR " + filename)
        Receive(ctrlstm)
        Dim f As FileStream = New FileStream (filename, FileMode.Create)
        Dim b(100000 - 1) As Byte
        Dim n As Integer
        While datastm.DataAvailable
            n = datastm.Read(b, 0, b.Length)
            f.Write(b, 0, n)
            Thread.Sleep(100)
        End While
        f.Close
        datastm.Close
        data.Close
    End Sub

    Public Function InitUpLoad(ByVal filename As String, ByVal binary As Boolean) As NetworkStream
        If binary Then
            Command(ctrlstm, "TYPE I")
        Else
            Command(ctrlstm, "TYPE A")
        End If
        SetupData
        Command(ctrlstm, "STOR " + filename)
        Receive(ctrlstm)
        Return datastm
    End Function
 
    Public Sub EndUpload
        datastm.Close
        data.Close
    End Sub

    Public Function InitDownLoad(ByVal filename As String, ByVal binary As Boolean) As NetworkStream
        If binary Then
            Command(ctrlstm, "TYPE I")
        Else
            Command(ctrlstm, "TYPE A")
        End If
        SetupData
        Command(ctrlstm, "RETR " + filename)
        Receive(ctrlstm)
        Return datastm
    End Function
 
    Public Sub EndDownload
        datastm.Close
        data.Close
    End Sub

    Public Function InitDirectUpLoad(ByVal binary As Boolean) As String
        If binary Then
            Command(ctrlstm, "TYPE I")
        Else
            Command(ctrlstm, "TYPE A")
        End If
        InitDirectUpload = SetupData1
    End Function
    
    Public Sub DirectUpLoad(ByVal filename As String)
        Command(ctrlstm, "STOR " + filename)
        Receive(ctrlstm)
    End Sub

    Public Sub InitDirectDownLoad(ByVal addr As String, ByVal binary As Boolean)
        If binary Then
            Command(ctrlstm, "TYPE I")
        Else
            Command(ctrlstm, "TYPE A")
        End If
        SetupData2(addr)
    End Sub
    
    Public Sub DirectDownLoad(ByVal filename As String)
        Command(ctrlstm, "RETR " + filename)
        Receive(ctrlstm)
    End Sub

    Public Sub Logout()
        Command(ctrlstm, "QUIT")
        ctrlstm.Close
        ctrl.Close
    End Sub
End Class

Public Class Fxp
    Public Shared Sub Transfer(fromhost As String, fromuser As String, frompass As String, frompath As String, _
                               filename As String, binary As Boolean, _
                               tohost As String, touser As String, topass As String, topath As String)
        Dim fromcli As FtpClient = New FtpClient(fromhost, fromuser, frompass)
        Dim tocli As FtpClient = New FtpClient(tohost, touser, topass)
        fromcli.ChDir(frompath)
        tocli.ChDir(topath)
        Dim istm As NetworkStream = fromcli.InitDownLoad(filename, binary)
        Dim ostm As NetworkStream = tocli.InitUpLoad(filename, binary)
        Dim b(100000 - 1) As Byte
        Dim n As Integer
        While istm.DataAvailable
            n = istm.Read(b, 0, b.Length)
            ostm.Write(b, 0, n)
            Thread.Sleep(100)
        End While
        fromcli.EndDownLoad
        tocli.EndUpLoad
        fromcli.Logout
        tocli.Logout
    End Sub

    Public Shared Sub TransferDirect(fromhost As String, fromuser As String, frompass As String, frompath As String, _
                                     filename As String, binary As Boolean, _
                                     tohost As String, touser As String, topass As String, topath As String)
        Dim fromcli As FtpClient = New FtpClient(fromhost, fromuser, frompass)
        Dim tocli As FtpClient = New FtpClient(tohost, touser, topass)
        fromcli.ChDir(frompath)
        tocli.ChDir(topath)
        fromcli.InitDirectDownload(tocli.InitDirectUpload(binary), binary)
        fromcli.DirectDownload(filename)
        tocli.DirectUpload(filename)
        fromcli.Logout
        tocli.Logout
    End Sub
End Class

Class TestClass
    Public Shared Sub Main(ByVal args As String())
        Fxp.TransferDirect("localhost", "anonymous", "arne@", "d1", "test.txt", False, "localhost", "anonymous", "arne@", "d2")
    End Sub
End Class
