April 19, 2014
Hot Topics:
RSS RSS feed Download our iPhone app

Create a GUI for an FTP Client with VB.NET

  • October 29, 2004
  • By Paul Kimmel
  • Send Email »
  • More Articles »

This Visual Basic Today column builds on the previous column, "Write an FTP Client with VB.NET to Bridge Legacy Software," which introduced an FTP client with some basic FTP capabilities. It extends that FTP client and begins the implementation of a Windows FTP GUI. I could probably write an entire book on building a Windows FTP application if I elected to cover design, implementation, patterns, GUI design techniques, and testing and deployment issues. However, I don't have that much time or enough space in this forum.

This column presents an assortment of skills you might need to complete the Windows FTP solution or similar kinds of applications. It further explores the TcpClient namespace and using new controls in Visual Studio .NET 2005. It also demonstrates a couple of patterns that you should know. Although the example it presents is implemented with VS.NET 2005, you could implement it in VS.NET 2003 with simple control substitutions.

Add Get File List to Your FTP Client

Client is one of those words that can be a bit ambiguous. Client can be code that talks to a server, but it also can mean GUI. (A less ambiguous term for GUI client is presentation layer.) This section refers to the client code and not a GUI.

"Write an FTP Client with VB.NET to Bridge Legacy Software" began the process of building an FTP client application. It implemented that client as a class library that could be used from any sort of application. Despite being 15 pages long in my word processor, that article still wasn't long enough to cover implementing all of the commands FTP servers support as defined by RFC (Request For Comment) 959. To make your presentation layer more interesting, implement another command, NLST, which returns a list of files on the FTP server.

To implement NLST, you need to add three more methods to your FtpClient:

  • GetFileList—directly implements the NLST command
  • CreateDataSocket—implements the PASV command and creates a second data socket (The PASV command tells the server to wait for a client to connect to it on a port the server designated.)
  • RequestResponse—returns the actual data that is returned from the server

RequestResponse is useful when you want to retrieve data like a list of files as opposed to server code. Listing 1 shows the implementation of GetFileList, CreateDataSocket, and RequestResponse.

Listing 1: GetFileList, CreateDataSocket, and RequestResponse (to be added to the FtpClient class)

Public Function GetFileList(ByVal mask As String) As String()
Const BUFFER_SIZE As Integer = 512
        Dim buffer(BUFFER_SIZE) As Byte

        Dim socket As Socket = Nothing
        Dim bytes As Integer
        Const separator As Char = "\n"
        Dim messageChunks As String()
        Dim temp As String = ""

        ' Implement this
        socket = CreateDataSocket()
        Try
            SendCommand("NLST " + mask)

            While (True)
                Array.Clear(buffer, 0, buffer.Length)
                bytes = socket.Receive(buffer, buffer.Length, 0)
                temp += ASCII.GetString(buffer, 0, bytes)
                If (bytes < buffer.Length) Then Exit While

            End While

            messageChunks = temp.Split(separator)
        Finally
            socket.Close()
        End Try

        Return messageChunks
End Function

Private Function CreateDataSocket() As Socket
Dim index1, index2, len, port, partCount As Integer
        Dim ipData, buf, ipAddress As String
        Dim parts(6) As Integer
        Dim ch As Char

        Dim socket As Socket
        Dim ep As IPEndPoint

        Dim reply As String = RequestResponse("PASV")
        index1 = reply.IndexOf("(")
        index2 = reply.IndexOf(")")
        ipData = reply.Substring(index1 + 1, index2 - index1 - 1)
        len = ipData.Length
        partCount = 0
        buf = ""

        Dim I As Integer = 0
        While (I <= len - 1 And partCount <= 6)
            ch = Char.Parse(ipData.Substring(I, 1))
            If (Char.IsDigit(ch)) Then
                buf += ch
            ElseIf (ch <> ",") Then
                Throw New IOException("Malformed PASV reply: " + _
                                      reply)
            End If

            If ((ch = ",") Or (I + 1 = len)) Then
                Try
                    parts(partCount) = Int32.Parse(buf)
                    partCount += 1
                    buf = ""
                Catch
                    Throw New IOException("Malformed PASV reply: " _
                                          + reply)
                End Try
            End If
            I += 1
        End While

        ipAddress = String.Format("{0}.{1}.{2}.{3}", parts(0), _
                    parts(1), parts(2), parts(3))

        port = parts(4) << 8
        port = port + parts(5)

        socket = New Socket(AddressFamily.InterNetwork, _
                            SocketType.Stream, ProtocolType.Tcp)
        ep = New IPEndPoint(Dns.Resolve(ipAddress).AddressList(0), _
                            port)

        Try
            socket.Connect(ep)
        Catch ex As Exception
            Throw New IOException("Cannot connect to remote _
                                   server", ex)
        End Try

        Return socket

End Function


Private Function RequestResponse(ByVal command As String) As String
        command += Environment.NewLine
        Dim commandBytes() As Byte = ASCII.GetBytes(command)
        clientSocket.Send(commandBytes, commandBytes.Length, 0)
        Return ReadLine()
End Function

I borrowed these methods from the Microsoft article "How to access a File Transfer Protocol Site by Using Visual Basic .NET," originally published as knowledge base article 832679 from Microsoft.com. They are a bit murkier than I like. Code like this often needs some careful refactoring and a couple of attempts in order to "prettify" it, but in this case, that would be a distraction with only cosmetic returns. (For more information on refactoring, refer to Martin Fowler's book, Refactoring: Improving the Design of Existing Code.)

If you want to learn more about FTP commands, refer to the supported and unsupported FTP commands on the Microsoft.com Web site. If you want to learn more about FTP status codes, refer to knowledge base article Q318380, "IIS Status Codes."





Page 1 of 3



Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Sitemap | Contact Us

Rocket Fuel