developer.com
Search EarthWeb
CodeGuru | Gamelan | Jars | Wireless | Discussions
Navigate developer.com
Architecture & Design  
Database  
Java
Languages & Tools
Microsoft & .NET
Open Source  
Project Management  
Security  
Techniques  
Voice  
Web Services  
Wireless/Mobile
XML  
Technology Jobs  

   Developer.com Webcasts:
  The Impact of Coding Standards and Code Reviews

  Project Management for the Developer

  Defining Your Own Software Development Methodology

  more Webcasts...




See the Winners!


Developer Jobs

Be a Commerce Partner
Promotional Gifts
Shop Online
Holiday Gift Ideas
Best Price
Computer Deals
Cell Phones
Prepaid Phone Card
Car Donations
KVM Switch over IP
Condos For Sale
Memory
Find Software
Phone Cards
Build a Server Rack

 
PDAs
PC Notebooks
Printers
Monitors

Click Here
Download these IBM resources today!
e-Kit: IBM Rational Systems Development Solution
With systems teams under so much pressure to develop products faster, reduce production costs, and react to changing business needs quickly, communication and collaboration seem to get lost. Now, theres a way to improve product quality and communication.

Webcast: Asset Reuse Strategies for Success--Innovate Don't Duplicate!
Searching for, identifying, updating, using and deploying software assets can be a difficult challenge.

eKit: Rational Build Forge Express
Access valuable resources to help you increase staff productivity, compress development cycles and deliver better software, fast.

Download: IBM Data Studio v1.1
Effectively design, develop, deploy and manage your data, databases, and database applications throughout the data management life.

eKit: Rational Asset Manager
Learn how to do more with your reusable assets, learn how Rational Asset Manager tracks and audits your assets in order to utilize them for reuse.
Related Article -
Write an FTP Client with VB.NET to Bridge Legacy Software
Developer News -
SaaS Tool Offers Custom Database Development    May 9, 2008
Microsoft’s Automated Agent: Can We Talk?    May 7, 2008
Borland Finally Sells CodeGear    May 7, 2008
Red Hat Heads For The JON 2.0    May 7, 2008
Free Tech Newsletter -

Best Practices for Developing a Web Site: Checklists, Tips, Strategies & More. Download Exclusive eBook Now.

Create a GUI for an FTP Client with VB.NET
By Paul Kimmel

Go to page: 1  2  3  Next  

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."

Go to page: 1  2  3  Next  


Tools:
Add www.developer.com to your favorites
Add www.developer.com to your browser search box
IE 7 | Firefox 2.0 | Firefox 1.5.x
Receive news via our XML/RSS feed


Visual Basic Archives

Work With InterSystems. Not Separate Systems. Rapidly develop and deploy connectable applications.
Learn about expanding business opportunities for the reseller channel. Visit IT Channel Planet.
Whitepaper: Enterprise Information Integration--Deployment Best Practices for Low-Cost Implementation
Data Sheet: IBM Information Server Blade
Guide to Developing a Web Site. Best Practices, Tips and Strategies. Download Exclusive eBook Now.



JupiterOnlineMedia

internet.comearthweb.comDevx.commediabistro.comGraphics.com

Search:

Jupitermedia Corporation has two divisions: Jupiterimages and JupiterOnlineMedia

Jupitermedia Corporate Info


Legal Notices, Licensing, Reprints, & Permissions, Privacy Policy.

Advertise | Newsletters | Tech Jobs | Shopping | E-mail Offers

Solutions
Whitepapers and eBooks
Microsoft Article: HyperV-The Killer Feature in WinServer ‘08
Avaya Article: How to Feed Data into the Avaya Event Processor
Microsoft Article: Install What You Need with Win Server ‘08
HP eBook: Putting the Green into IT
Whitepaper: HP Integrated Citrix XenServer for HP ProLiant Servers
Intel Go Parallel Portal: Interview with C++ Guru Herb Sutter, Part 1
Intel Go Parallel Portal: Interview with C++ Guru Herb Sutter, Part 2--The Future of Concurrency
Avaya Article: Setting Up a SIP A/S Development Environment
IBM Article: How Cool Is Your Data Center?
Microsoft Article: Managing Virtual Machines with Microsoft System Center
HP eBook: Storage Networking , Part 1
Microsoft Article: Solving Data Center Complexity with Microsoft System Center Configuration Manager 2007
MORE WHITEPAPERS, EBOOKS, AND ARTICLES
Webcasts
Intel Video: Are Multi-core Processors Here to Stay?
On-Demand Webcast: Five Virtualization Trends to Watch
HP Video: Page Cost Calculator
Intel Video: APIs for Parallel Programming
HP Webcast: Storage Is Changing Fast - Be Ready or Be Left Behind
Microsoft Silverlight Video: Creating Fading Controls with Expression Design and Expression Blend 2
MORE WEBCASTS, PODCASTS, AND VIDEOS
Downloads and eKits
Sun Download: Solaris 8 Migration Assistant
Sybase Download: SQL Anywhere Developer Edition
Red Gate Download: SQL Backup Pro and free DBA Best Practices eBook
Red Gate Download: SQL Compare Pro 6
Iron Speed Designer Application Generator
MORE DOWNLOADS, EKITS, AND FREE TRIALS
Tutorials and Demos
How-to-Article: Preparing for Hyper-Threading Technology and Dual Core Technology
eTouch PDF: Conquering the Tyranny of E-Mail and Word Processors
IBM Article: Collaborating in the High-Performance Workplace
HP Demo: StorageWorks EVA4400
Intel Featured Algorhythm: Intel Threading Building Blocks--The Pipeline Class
Microsoft How-to Article: Get Going with Silverlight and Windows Live
MORE TUTORIALS, DEMOS AND STEP-BY-STEP GUIDES