Contact Info

Crumbtrail

ActiveXperts.com » Network Component » How to Use Network Component » TFTP » Visual Basic 6

Visual Basic TFTP Client Sample Source Code

Network Component provides an easy-to-use development interface to a variety of IP protocols. By using Network Component, you can very easily create or enhance applications with network features.

Network Component features the following: DNS, FTP, HTTP, HTTPs, ICMP Ping, IP-to-Country, MSN, NTP, RSH, SCP, SFTP, SNMP v1/v2c (Get, GetNext, Set), SNMP Traps, SNMP MIB, SSH, TCP, Telnet, TFTP, UDP, Telnet, Wake-On-LAN and more.

Network Component can be well integrated into any development platform that supports ActiveX objects.


This document describes how the Network Component FtpServer object can be integrated into your projects.

The most important functions of the FtpServer object are:


Step 1: Download and install the Network Component

Download Network Component from the ActiveXperts Download Site and start the installation. The installation guides you through the installation process.

Step 2: Create a new Visual Basic project

Launch 'Microsoft Visual Basic' from the Start menu, and choose 'New' from the 'File Menu'. The 'New Project' dialog appears.
Select 'Standard Exe' and click 'OK':

Visual Basic

(Click on the picture to enlarge)

Step 3: Refer to the Network Component Library and create the objects

A new Project is created, with a blank form.

First, you must add a reference to Network Component in the project to be able to use the object. To do so, choose 'References...' from the 'Project' menu. In the 'References' dialog that pops up, enable the 'Network Component 3.1 Type Library' reference as shown in the following picture:

(Click on the picture to enlarge)

Click 'OK' to close the 'References...' dialog.

Then, select the Project form and choose 'View Code' from the context menu:

(Click on the picture to enlarge)

On top of your code, declare the following object:

Public objTftpServer As AxNetwork.TftpServer

Step 4: Create the object

From the Code window, select 'Form'. The Private Sub 'Form_Load()' will be displayed now. In the 'Form Load' function, create the object in the following way:

Set objTftpServer = CreateObject("AxNetwork.TftpServer")

Appendix: Full source code

Option Explicit

'///////////////////////////////////////////////////////////////////////

Dim objTftp As AxNetwork.TftpServer

'///////////////////////////////////////////////////////////////////////

Private Declare Function GetTempPath Lib "kernel32" Alias "GetTempPathA" (ByVal nBufferLength As Long, ByVal lpBuffer As String) As Long
Private Const MAX_PATH = 260

'///////////////////////////////////////////////////////////////////////

Private Sub Form_Load()
    Set objTftp = CreateObject("AxNetwork.TftpServer")
    SetDefaultLogFile
    rbFromLocalToServer = True
    UpdateControls
End Sub

'///////////////////////////////////////////////////////////////////////

Private Sub btnCopy_Click()
    objTftp.Clear
    objTftp.LogFile = txtLogfile.Text
    objTftp.HostPort = CInt(txtPort.Text)
    objTftp.BinaryTransfer = True
    
    MousePointer = vbHourglass
    
    If (rbFromServerToLocal) Then
        objTftp.Get txtHost.Text, txtFrom.Text, txtTo.Text
    Else
        objTftp.Put txtHost.Text, txtFrom.Text, txtTo.Text
    End If
    
    MousePointer = vbDefault
    GetResult
End Sub

'///////////////////////////////////////////////////////////////////////

Public Function FileExists(sFileName As String) As Boolean
  FileExists = CBool(Len(Dir$(sFileName))) And CBool(Len(sFileName))
End Function

'///////////////////////////////////////////////////////////////////////

Private Function SetDefaultLogFile()
    Dim Buffer As String
    Buffer = Space(MAX_PATH)
    
    If GetTempPath(MAX_PATH, Buffer) <> 0 Then
        txtLogfile.Text = Left$(Buffer, InStr(Buffer, vbNullChar) - 1) & "Tftp.log"
    Else
        txtLogfile.Text = "C:\Tfpt.log"
    End If
End Function

'///////////////////////////////////////////////////////////////////////

Private Function GetResult()
    GetResult = objTftp.LastError
    txtResult.Text = objTftp.LastError & ": " & objTftp.GetErrorDescription(objTftp.LastError)
End Function

'///////////////////////////////////////////////////////////////////////

Private Sub UpdateControls()
    btnToBrowse.Enabled = rbFromServerToLocal
    btnFromBrowse.Enabled = rbFromLocalToServer
End Sub

'///////////////////////////////////////////////////////////////////////

Private Sub btnToBrowse_Click()
    dlgOpenfile.ShowOpen
    txtTo.Text = dlgOpenfile.FileName
End Sub

'///////////////////////////////////////////////////////////////////////

Private Sub btnFromBrowse_Click()
    dlgOpenfile.ShowOpen
    txtFrom.Text = dlgOpenfile.FileName
End Sub

'///////////////////////////////////////////////////////////////////////

Private Sub btnView_Click()
    If FileExists(txtLogfile.Text) = True Then
        Shell "notepad " + txtLogfile.Text, vbNormalFocus
    End If
End Sub

'///////////////////////////////////////////////////////////////////////

Private Sub rbFromLocalToServer_Click()
    UpdateControls
End Sub

'///////////////////////////////////////////////////////////////////////

Private Sub rbFromServerToLocal_Click()
    UpdateControls
End Sub

'///////////////////////////////////////////////////////////////////////

You can download the complete samples here. There are many other working Network Component scripts on our site and shipped with the product.

NOTE: Demo Projects are created with Microsoft Visual Studio 2008

The Network Component project ships with a set of Microsoft Visual Studio .NET samples, including samples for Microsoft Visual C# .NET. The projects are created with Microsoft Visual Studio 2008.

Users with a later version of Microsoft Visual Studio can open such a project. The Visual Studio Conversion Wizard will guide you through the process of converting the project to the version used.