July 23, 2014
Hot Topics:
RSS RSS feed Download our iPhone app

Write an FTP Client with VB.NET to Bridge Legacy Software

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

Testing the FTP Client

Next, you need to test the client. A simple test is creating an instance of the client, attaching some event handlers to the FtpClient's public events, and invoking some methods. A great way to test a class library like the FtpClient is with NUnit, but I use a simple console application here (see Listing 5).

Listing 5: A Console Application That Tests Some Capabilities of the FtpClient

Imports Ftp

Module Module1

    Sub Main()
        Dim client As FtpClient = New FtpClient()
        AddHandler client.OnConnectionEvent, _
                   AddressOf OnConnection
        AddHandler client.onRawDataReceivedEvent, _
                   AddressOf OnRawDataReceived
        AddHandler client.OnAuthentication, _
                   AddressOf OnAuthenticated

        client.Connect()
        client.Login()

        Console.WriteLine("press enter to exit")
        Console.ReadLine()
        client.Disconnect()
    End Sub

    Public Sub OnConnection(ByVal sender As Object, _
                            ByVal e As ConnectionEventArgs)
        Console.WriteLine("Connected: " & e.Connected.ToString)
    End Sub

    Public Sub OnRawDataReceived(ByVal sender As Object, _
    ByVal e As RawDataReceivedEventArgs)
        Console.WriteLine("Received: " & e.RawData)
    End Sub

    Public Sub OnAuthenticated(ByVal sender As Object, _
    ByVal e As AuthenticationEventArgs)
        Console.WriteLine("Logged in: " + e.Authenticated.ToString())
    End Sub


End Module

Before running the console application, make sure you add a reference to the FtpClient class library and import the FTP namespace.

The sample test program tests and responds to the OnConnectionEvent, OnRawDataReceivedEvent, OnAuthenticationEvent, and invokes the Connect, Login, and Disconnect methods. If everything is working, running the console application should produce output that looks something like Figure 4.



Click here for a larger image.

Figure 4: Output from Our Test Console Application

The next step is implementing more of the features you need. Not all implementations need every FTP feature. For example, a general-purpose FTP client might need most of the FTP commands, but simply uploading data as part of a process may need only the ability to change directories and upload a file.

Finally, for commercial or private business implementations, additional tests are helpful. For example, you might need to add exception handlers to respond to a timeout, an unavailable server or folder, or unexpected failures. The devil (and the time) are always in the details.

Writing Connected Software Simply

This article provides a lot of juicy but pretty straightforward code. The most important thing to note is how relatively easy it is to use sockets in .NET. Create a socket, an IPEndPoint, know a little bit about the data you will be receiving from the other end point, and you are well on your way to writing connected software.

This article exemplifies the most important thing about great frameworks: They make programming complex tasks much easier. Without needing to be an RS232, TCP, or sockets expert, one can get down to the business of coding a connected solution at a higher level of abstraction. Ultimately, the .NET framework brings complex solutions—whether in TCP or sockets programming, XML Web services, or advanced graphics programming—within everyone's grasp.

Biography

Paul Kimmel is the VB Today columnist, has written several books on .NET programming, and is a software architect. You may contact him at pkimmel@softconcepts.com if you need assistance developing software or are interested in joining the Lansing Area .NET Users Group (glugnet.org).

Copyright © 2004 by Paul Kimmel. All Rights Reserved.





Page 4 of 4



Comment and Contribute

 


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

 

 


Sitemap | Contact Us

Rocket Fuel