Back  | Code Snippets |  [Bill's Home]

FTP client

FTP Client ZIP

THIS PAGE HAS STILL TO BE FORMATTED.

Most programming language have FTP components which implement most of the FTP protocol, and allow the programmer to make simple calls to the component. In VB the main component is Inet. It has the following properties, events and methods.

Properties

AccessType This set sets or returns a value which is used to determine whether a proxy is used (icNamedProxy) or not (icDirect). If the default setting is set in the register then the property is set to icUseDefault.
Document This sets or returns the name of the file or document that will be used with the Execute method. When it is not set, the default document is returned from the FTP server.
Name This returns the name that is used in the code to identify the FTP object.
Proxy This sets or returns the name of the proxy server used to communicate with the Internet.
RequestTimeOut This sets or returns or sets the time length, in seconds, to wait before a time-out expires.
ResponseCode This returns the error code from an icError state occurs in the State-Changed event. More information is stored in the ResponseInfo property.
ResponseInfo This return the text message for the last known error.
URL This sets or returns URL that is used by the Execute or OpenURL methods.
UserName This sets or returns the name that is sent with requests to remote com-puters.

Methods

The main methods used in FTP programming are:

Cancel This method cancels the current transaction, and closes down any opened connections.
Execute This method executes a request on the server. In FTP, typical requests are GET, PWD, and so on.
OpenURL This method returns or opens and returns a document at the specified URL.

The format of the Execute method is:

object.Execute url, operation, data

The main operations are:

Operation Description
CD dir1 Change directory (dir1).
CDUP Change to parent directory.
CLOSE Closes current FTP connection.
DELETE file Deletes file.
DIR List directory command.
GET file1 file2 Gets a remote file (file1), and creates as a new local file (file2).
LS List directory
MKDIR dir1 Creates a directory (dir1).
PUT file1 file2 Puts a local file (file1) to the remote host (file2).
PWD Shows the current working directory.
QUIT Quit the session
RECV file1 file2 Retrieves the remote file (file1), and creates a new local file (file2).
RENAME file1 file2 Renames the remote file (file1) to the new name (file2).
RMDIR dir1 Remove a directory (dir1).
SEND file1 file2 Same as PUT.
SIZE dir1 Returns the size of the directory (dir1).

Events

The only event associated with the Inet object is StateChanged. It is called with:

object_StateChanged(ByVal State As Integer)

Its setting are:

Constant Value Description
icNone 0 No state to report.
icHostResolvingHost 1 Resolving the IP address of the host
icHostResolved 2 Resolved the IP address of the host
icConnecting 3 Connecting to the host.
icConnected 4 Successfully connected to the host.
icRequesting 5 Sending a request to the host.
icRequestSent 6 Successfully sent the request.
icReceivingResponse 7 Receiving a response from the host.
icResponseReceived 8 Successfully received a response from the host.
icDisconnecting 9 Disconnecting from the host computer.
icDisconnected 10 Successfully disconnected from the host computer.
icError 11 Error occurred in communicating with the host.
icResponseCompleted 12 Request has completed and all data has been received.

For example for an Inet1 object, the following could be used:

Private Sub Inet1_StateChanged(ByVal State As Integer)
Dim Msg As String

Select Case State
Case icNone
' Ignore, no error
Case icHostResolvingHost
Msg = "Resolving Host... "

' Other settings are inserted here

Case icError ' 11
Msg = "ErrorCode: "&Inet1.ResponseCode&" : "&Inet1.ResponseInfo
End Select
End Sub

Program

A sample run of the program is shown next:

The code for the FTP client form is:

Private Sub button_connect_Click()
Inet1.AccessType = icUseDefault
Inet1.URL = text1.Text
Inet1.UserName = Text2.Text
Inet1.Password = Text3.Text
End Sub

Private Sub button_help_Click()
frmBrowser.Show
End Sub

Private Sub button_exit_Click()
End
End Sub

Private Sub button_dir_Click()
Inet1.Execute , "LS"
command_window.Text = command_window.Text + "LS:" + vbCrLf
End Sub

Private Sub button_close_Click()
Inet1.Execute , "CLOSE"
command_window.Text = command_window.Text + "CLOSE:" + vbCrLf
End Sub

Private Sub button_pwd_Click()
Inet1.Execute , "PWD"
command_window.Text = command_window.Text + "PWD:" + vbCrLf
End Sub

Private Sub button_size_Click()
Inet1.Execute , "SIZE ."
command_window.Text = command_window.Text + "SIZE:" + vbCrLf
End Sub

Private Sub button_mkdir_Click()
Inet1.Execute , "MKDIR TEMP"
command_window.Text = command_window.Text + "Mkdir temp:" + vbCrLf
End Sub

Private Sub button_rmdir_Click()
Inet1.Execute , "RMDIR TEMP"
command_window.Text = command_window.Text + "Rmdir temp:" + vbCrLf
End Sub

Private Sub button_status_Click()
status.Text = ""
End Sub

Private Sub Form_Load()

End Sub

Private Sub Inet1_StateChanged(ByVal State As Integer)
Select Case State
Case icNone
' Ignore, no error
Case icHostResolvingHost
status.Text = status.Text + "Resolving host... " + vbCrLf
Case icHostResolved
status.Text = status.Text + "Resolved host... " + vbCrLf
Case icConnecting
status.Text = status.Text + "Connecting to host... " + vbCrLf
Case icConnected
status.Text = status.Text + "Connected to host... " + vbCrLf

Case icError
status.Text = status.Text + "ErrorCode: " & Inet1.ResponseCode & " : " & Inet1.ResponseInfo

Case icResponseCompleted
Dim data As Variant
data = Inet1.GetChunk(1024, icString)
Do While LenB(data) > 0
status.Text = status.Text + data
data = Inet1.GetChunk(1024, icString)
Loop
status.Text = status.Text + vbCrLf
End Select

End Sub

Private Sub text_ftp_command_Change()
Inet1.Execute , text_ftp_command.Text
command_window.Text = command_window.Text + Text4.Text
End Sub