Access an FTP Site - Receipe problem

garcon

Well-known member
Joined
Dec 13, 2004
Messages
47
Programming Experience
Beginner
Folks,

This is a recipe for accessing an FTP site. I having problems with the "Send" method and th "IPAddress" method - my compiler says: "Name Send is not declared" and "Name IPAddress is not declared"

Can anyone make any intelligent suggestions please?

**** CODE AS FOLLOWS ****



VB.NET:
[size=2][color=#0000ff]Public[/color][/size][size=2][color=#0000ff]Class[/color][/size][size=2] FtpClient
 
[/size][size=2][color=#0000ff]Inherits[/color][/size][size=2] System.ComponentModel.Component
 
[/size][size=2][color=#008000]'The internal TCP connection
 
[/color][/size][size=2][color=#0000ff]Private[/color][/size][size=2] Client [/size][size=2][color=#0000ff]As[/color][/size][size=2][color=#0000ff]New[/color][/size][size=2] System.Net.Sockets.TcpClient
 
[/size][size=2][color=#0000ff]Private[/color][/size][size=2] Stream [/size][size=2][color=#0000ff]As[/color][/size][size=2] System.Net.Sockets.NetworkStream
 
 
 
[/size][size=2][color=#008000]'The connection state
 
[/color][/size][size=2][color=#0000ff]Private[/color][/size][size=2] _Connected [/size][size=2][color=#0000ff]As[/color][/size][size=2][color=#0000ff]Boolean[/color][/size][size=2] = [/size][size=2][color=#0000ff]False
 
[/color][/size][size=2][color=#0000ff]Public[/color][/size][size=2][color=#0000ff]ReadOnly[/color][/size][size=2][color=#0000ff]Property[/color][/size][size=2] Connected() [/size][size=2][color=#0000ff]As[/color][/size][size=2][color=#0000ff]Boolean
 
[/color][/size][size=2][color=#0000ff]Get
 
[/color][/size][size=2][color=#0000ff]Return[/color][/size][size=2] _Connected
 
[/size][size=2][color=#0000ff]End[/color][/size][size=2][color=#0000ff]Get
 
[/color][/size][size=2][color=#0000ff]End[/color][/size][size=2][color=#0000ff]Property
 
 
 
[/color][/size][size=2][color=#008000]'Some command constants
 
[/color][/size][size=2][color=#0000ff]Private[/color][/size][size=2][color=#0000ff]Class[/color][/size][size=2] Commands
 
[/size][size=2][color=#0000ff]Public[/color][/size][size=2][color=#0000ff]Const[/color][/size][size=2] User [/size][size=2][color=#0000ff]As[/color][/size][size=2][color=#0000ff]String[/color][/size][size=2] = "USER "
 
[/size][size=2][color=#0000ff]Public[/color][/size][size=2][color=#0000ff]Const[/color][/size][size=2] Password [/size][size=2][color=#0000ff]As[/color][/size][size=2][color=#0000ff]String[/color][/size][size=2] = "PASS "
 
[/size][size=2][color=#0000ff]Public[/color][/size][size=2][color=#0000ff]Const[/color][/size][size=2] Quit [/size][size=2][color=#0000ff]As[/color][/size][size=2][color=#0000ff]String[/color][/size][size=2] = "QUIT" & vbNewLine
 
[/size][size=2][color=#0000ff]Public[/color][/size][size=2][color=#0000ff]Const[/color][/size][size=2] GetFile [/size][size=2][color=#0000ff]As[/color][/size][size=2][color=#0000ff]String[/color][/size][size=2] = "RETR "
 
[/size][size=2][color=#0000ff]Public[/color][/size][size=2][color=#0000ff]Const[/color][/size][size=2] UsePassiveMode [/size][size=2][color=#0000ff]As[/color][/size][size=2][color=#0000ff]String[/color][/size][size=2] = "PASV" & vbNewLine
 
[/size][size=2][color=#0000ff]Public[/color][/size][size=2][color=#0000ff]Const[/color][/size][size=2] UseBinary [/size][size=2][color=#0000ff]As[/color][/size][size=2][color=#0000ff]String[/color][/size][size=2] = "TYPE I" & vbNewLine
 
[/size][size=2][color=#0000ff]Public[/color][/size][size=2][color=#0000ff]Const[/color][/size][size=2] UseAscii [/size][size=2][color=#0000ff]As[/color][/size][size=2][color=#0000ff]String[/color][/size][size=2] = "TYPE A" & vbNewLine
 
[/size][size=2][color=#0000ff]End[/color][/size][size=2][color=#0000ff]Class
 
 
 
[/color][/size][size=2][color=#0000ff]Private[/color][/size][size=2][color=#0000ff]Enum[/color][/size][size=2] ReturnCodes
 
ServiceReady = 220
 
Accepted = 200
 
PasswordRequired = 331
 
UserLoggedIn = 230
 
EnteringPassivemode = 227
 
StartingTransferAlreadyOpen = 125
 
StartingTransferOpening = 150
 
TransferComplete = 226
 
[/size][size=2][color=#0000ff]End[/color][/size][size=2][color=#0000ff]Enum
 
[/color][/size][size=2][color=#0000ff]Private[/color][/size][size=2][color=#0000ff]Function[/color][/size][size=2] GetResponse() [/size][size=2][color=#0000ff]As[/color][/size][size=2][color=#0000ff]String
 
[/color][/size][size=2][color=#008000]'Retrieve all available lines
 
[/color][/size][size=2][color=#0000ff]Dim[/color][/size][size=2] Character [/size][size=2][color=#0000ff]As[/color][/size][size=2][color=#0000ff]String
 
[/color][/size][size=2][color=#0000ff]Dim[/color][/size][size=2] Response [/size][size=2][color=#0000ff]As[/color][/size][size=2][color=#0000ff]String[/color][/size][size=2] = ""
 
[/size][size=2][color=#0000ff]Do
 
[/color][/size][size=2][color=#0000ff]Do
 
[/color][/size][size=2]Character = Chr(Stream.ReadByte()).ToString()
 
Response &= Character
 
[/size][size=2][color=#0000ff]Loop[/color][/size][size=2][color=#0000ff]Until[/color][/size][size=2] Character = Chr(10)
 
[/size][size=2][color=#0000ff]Loop[/color][/size][size=2][color=#0000ff]While[/color][/size][size=2] Stream.DataAvailable
 
Response = Response.Trim([/size][size=2][color=#0000ff]New[/color][/size][size=2][color=#0000ff]Char[/color][/size][size=2]() {Chr(13), Chr(10)})
 
System.Diagnostics.Debug.WriteLine(Response)
 
[/size][size=2][color=#0000ff]Return[/color][/size][size=2] Response
 
[/size][size=2][color=#0000ff]End[/color][/size][size=2][color=#0000ff]Function
 
[/color][/size][size=2][color=#0000ff]Private[/color][/size][size=2][color=#0000ff]Function[/color][/size][size=2] CheckCode([/size][size=2][color=#0000ff]ByVal[/color][/size][size=2] response [/size][size=2][color=#0000ff]As[/color][/size][size=2][color=#0000ff]String[/color][/size][size=2], [/size][size=2][color=#0000ff]ByVal[/color][/size][size=2] expectedCode [/size][size=2][color=#0000ff]As[/color][/size][size=2] ReturnCodes) [/size][size=2][color=#0000ff]As[/color][/size][size=2][color=#0000ff]Boolean
 
[/color][/size][size=2][color=#0000ff]Return[/color][/size][size=2] Val(response.Substring(0, 3)) = expectedCode
 
[/size][size=2][color=#0000ff]End[/color][/size][size=2][color=#0000ff]Function
 
[/color][/size][size=2][color=#0000ff]Public[/color][/size][size=2][color=#0000ff]Sub[/color][/size][size=2] Connect([/size][size=2][color=#0000ff]ByVal[/color][/size][size=2] serverName [/size][size=2][color=#0000ff]As[/color][/size][size=2][color=#0000ff]String[/color][/size][size=2], [/size][size=2][color=#0000ff]ByVal[/color][/size][size=2] userName [/size][size=2][color=#0000ff]As[/color][/size][size=2][color=#0000ff]String[/color][/size][size=2], [/size][size=2][color=#0000ff]ByVal[/color][/size][size=2] password [/size][size=2][color=#0000ff]As[/color][/size][size=2][color=#0000ff]String[/color][/size][size=2])
 
[/size][size=2][color=#0000ff]If[/color][/size][size=2] Connected [/size][size=2][color=#0000ff]Then[/color][/size][size=2][color=#0000ff]Me[/color][/size][size=2].Disconnect()
 
[/size][size=2][color=#008000]'Connect to POP3 server
 
[/color][/size][size=2][color=#008000]'which is always at port 21)
 
[/color][/size][size=2]Client.Connect(serverName, 21)
 
Stream = Client.GetStream()
 
[/size][size=2][color=#008000]'Send user name
 
[/color][/size][size=2][color=#0000ff]Dim[/color][/size][size=2] Response [/size][size=2][color=#0000ff]As[/color][/size][size=2][color=#0000ff]String
 
[/color][/size][size=2]Response = GetResponse()
 
Response = Send(Commands.User & userName & vbNewLine)
 
[/size][size=2][color=#0000ff]If[/color][/size][size=2] CheckCode(Response, ReturnCodes.PasswordRequired) [/size][size=2][color=#0000ff]Then
 
[/color][/size][size=2][color=#008000]'Send password
 
[/color][/size][size=2]Response = Send(Commands.Password & password & vbNewLine)
 
[/size][size=2][color=#0000ff]End[/color][/size][size=2][color=#0000ff]If
 
[/color][/size][size=2][color=#0000ff]If[/color][/size][size=2][color=#0000ff]Not[/color][/size][size=2] (CheckCode(Response, ReturnCodes.UserLoggedIn)) [/size][size=2][color=#0000ff]And[/color][/size][size=2][color=#0000ff]Not[/color][/size][size=2] (CheckCode(Response, ReturnCodes.ServiceReady)) [/size][size=2][color=#0000ff]Then
 
[/color][/size][size=2][color=#0000ff]Throw[/color][/size][size=2][color=#0000ff]New[/color][/size][size=2] ApplicationException("Could not log in")
 
[/size][size=2][color=#0000ff]End[/color][/size][size=2][color=#0000ff]If
 
[/color][/size][size=2]_Connected = [/size][size=2][color=#0000ff]True
 
[/color][/size][size=2][color=#0000ff]End[/color][/size][size=2][color=#0000ff]Sub
 
[/color][/size][size=2][color=#0000ff]Public[/color][/size][size=2][color=#0000ff]Sub[/color][/size][size=2] Disconnect()
 
[/size][size=2][color=#0000ff]If[/color][/size][size=2] Connected [/size][size=2][color=#0000ff]Then
 
[/color][/size][size=2][color=#0000ff]If[/color][/size][size=2][color=#0000ff]Not[/color][/size][size=2] TransferClient [/size][size=2][color=#0000ff]Is[/color][/size][size=2][color=#0000ff]Nothing[/color][/size][size=2][color=#0000ff]Then
 
[/color][/size][size=2]TransferClient.Close()
 
[/size][size=2][color=#0000ff]End[/color][/size][size=2][color=#0000ff]If
 
[/color][/size][size=2]Send(Commands.Quit)
 
_Connected = [/size][size=2][color=#0000ff]False
 
[/color][/size][size=2][color=#0000ff]End[/color][/size][size=2][color=#0000ff]If
 
[/color][/size][size=2][color=#0000ff]End[/color][/size][size=2][color=#0000ff]Sub
 
[/color][/size][size=2][color=#0000ff]Private[/color][/size][size=2] TransferClient [/size][size=2][color=#0000ff]As[/color][/size][size=2] System.Net.Sockets.TcpClient
 
[/size][size=2][color=#0000ff]Private[/color][/size][size=2] TransferEndpoint [/size][size=2][color=#0000ff]As[/color][/size][size=2] System.Net.IPEndPoint
 
 
 
 
 
[/size][size=2][color=#0000ff]Private[/color][/size][size=2][color=#0000ff]Sub[/color][/size][size=2] CreateTransferClient()
 
[/size][size=2][color=#0000ff]Dim[/color][/size][size=2] Response [/size][size=2][color=#0000ff]As[/color][/size][size=2][color=#0000ff]String[/color][/size][size=2] = Send(Commands.UsePassiveMode)
 
[/size][size=2][color=#0000ff]If[/color][/size][size=2][color=#0000ff]Not[/color][/size][size=2] CheckCode(Response, ReturnCodes.EnteringPassivemode) [/size][size=2][color=#0000ff]Then
 
[/color][/size][size=2][color=#0000ff]Throw[/color][/size][size=2][color=#0000ff]New[/color][/size][size=2] ApplicationException("Error entering passive mode")
 
[/size][size=2][color=#0000ff]End[/color][/size][size=2][color=#0000ff]If
 
[/color][/size][size=2][color=#008000]'The IP address and port number is appended to the response
 
[/color][/size][size=2][color=#008000]'Retrieve these details
 
[/color][/size][size=2][color=#0000ff]Dim[/color][/size][size=2] StartPos [/size][size=2][color=#0000ff]As[/color][/size][size=2][color=#0000ff]Integer[/color][/size][size=2] = Response.IndexOf("(")
 
[/size][size=2][color=#0000ff]Dim[/color][/size][size=2] EndPos [/size][size=2][color=#0000ff]As[/color][/size][size=2][color=#0000ff]Integer[/color][/size][size=2] = Response.IndexOf(")")
 
[/size][size=2][color=#0000ff]Dim[/color][/size][size=2] IPAndPort [/size][size=2][color=#0000ff]As[/color][/size][size=2][color=#0000ff]String[/color][/size][size=2] = Response.Substring(StartPos + 1, EndPos - StartPos - 1)
 
[/size][size=2][color=#0000ff]Dim[/color][/size][size=2] IPParts() [/size][size=2][color=#0000ff]As[/color][/size][size=2][color=#0000ff]String[/color][/size][size=2] = IPAndPort.Split(","c)
 
[/size][size=2][color=#0000ff]Dim[/color][/size][size=2] IP [/size][size=2][color=#0000ff]As[/color][/size][size=2][color=#0000ff]String[/color][/size][size=2] = IPParts(0) + "." + IPParts(1) + "." + IPParts(2) + "." + IPParts(3)
 
[/size][size=2][color=#0000ff]Dim[/color][/size][size=2] Port [/size][size=2][color=#0000ff]As[/color][/size][size=2][color=#0000ff]Integer[/color][/size][size=2] = Convert.ToInt32(IPParts(4)) * 256 + Convert.ToInt32(IPParts(5))
 
[/size][size=2][color=#008000]'Create the date transfer conection
 
[/color][/size][size=2]TransferClient = [/size][size=2][color=#0000ff]New[/color][/size][size=2] System.Net.Sockets.TcpClient
 
TransferEndpoint = [/size][size=2][color=#0000ff]New[/color][/size][size=2] System.Net.IPEndPoint([/size][size=2][color=#ff0000]IPAddress[/color][/size][size=2].Parse(IP), Port)
 
[/size][size=2][color=#0000ff]End[/color][/size][size=2][color=#0000ff]Sub
 
 
 
[/color][/size][size=2][color=#0000ff]Private[/color][/size][size=2][color=#0000ff]Sub[/color][/size][size=2] SetMode([/size][size=2][color=#0000ff]ByVal[/color][/size][size=2] binaryMode [/size][size=2][color=#0000ff]As[/color][/size][size=2][color=#0000ff]Boolean[/color][/size][size=2])
 
[/size][size=2][color=#0000ff]Dim[/color][/size][size=2] Response [/size][size=2][color=#0000ff]As[/color][/size][size=2][color=#0000ff]String
 
[/color][/size][size=2][color=#0000ff]If[/color][/size][size=2] binaryMode [/size][size=2][color=#0000ff]Then
 
[/color][/size][size=2]Response = Send(Commands.UseBinary)
 
[/size][size=2][color=#0000ff]Else
 
[/color][/size][size=2]Response = Send(Commands.UseAscii)
 
[/size][size=2][color=#0000ff]End[/color][/size][size=2][color=#0000ff]If
 
[/color][/size][size=2][color=#0000ff]If[/color][/size][size=2][color=#0000ff]Not[/color][/size][size=2] CheckCode(Response, ReturnCodes.Accepted) [/size][size=2][color=#0000ff]Then
 
[/color][/size][size=2][color=#0000ff]Throw[/color][/size][size=2] ([/size][size=2][color=#0000ff]New[/color][/size][size=2] ApplicationException("Could not change mode"))
 
[/size][size=2][color=#0000ff]End[/color][/size][size=2][color=#0000ff]If
 
[/color][/size][size=2][color=#0000ff]End[/color][/size][size=2][color=#0000ff]Sub
 
[/color][/size][size=2][color=#0000ff]Public[/color][/size][size=2][color=#0000ff]Function[/color][/size][size=2] DownlaodFile([/size][size=2][color=#0000ff]ByVal[/color][/size][size=2] filename [/size][size=2][color=#0000ff]As[/color][/size][size=2][color=#0000ff]String[/color][/size][size=2], [/size][size=2][color=#0000ff]ByVal[/color][/size][size=2] binaryMode [/size][size=2][color=#0000ff]As[/color][/size][size=2][color=#0000ff]Boolean[/color][/size][size=2]) [/size][size=2][color=#0000ff]As[/color][/size][size=2] NetworkStream
 
[/size][size=2][color=#008000]'Create a connection to the second port in passive mode
 
[/color][/size][size=2]CreateTransferClient()
 
TransferClient.Connect(TransferEndPoint)
 
SetMode(binaryMode)
 
[/size][size=2][color=#0000ff]Dim[/color][/size][size=2] Response [/size][size=2][color=#0000ff]As[/color][/size][size=2][color=#0000ff]String[/color][/size][size=2] = Send(Commands.GetFile & filename * vbNewLine)
 
 
 
[/size][size=2][color=#0000ff]If[/color][/size][size=2][color=#0000ff]Not[/color][/size][size=2] CheckCode(Response, ReturnCodes.StartingTransferAlreadyOpen) [/size][size=2][color=#0000ff]And[/color][/size][size=2][color=#0000ff]Not[/color][/size][size=2] (CheckCode(Response, ReturnCodes.StartingTransferOpening)) [/size][size=2][color=#0000ff]Then
 
[/color][/size][size=2][color=#0000ff]Throw[/color][/size][size=2][color=#0000ff]New[/color][/size][size=2] ApplicationException("Could not open connection")
 
[/size][size=2][color=#0000ff]End[/color][/size][size=2][color=#0000ff]If
 
[/color][/size][size=2][color=#008000]'Let the client read data fron the network stream
 
[/color][/size][size=2][color=#008000]'This is more efficient than creating and returning an
 
[/color][/size][size=2][color=#008000]'intermediate byte array, but it also relies on the client
 
[/color][/size][size=2][color=#008000]'to close the stream
 
[/color][/size][size=2][color=#0000ff]Return[/color][/size][size=2] TransferClient.GetStream()
 
[/size][size=2][color=#0000ff]End[/color][/size][size=2][color=#0000ff]Function
 
 
 
[/color][/size][size=2][color=#0000ff]Public[/color][/size][size=2][color=#0000ff]Sub[/color][/size][size=2] ConfirmDownLoadComplete()
 
[/size][size=2][color=#0000ff]Dim[/color][/size][size=2] Response [/size][size=2][color=#0000ff]As[/color][/size][size=2][color=#0000ff]String[/color][/size][size=2] = GetResponse()
 
CheckCode(Response, ReturnCodes.TransferComplete)
 
[/size][size=2][color=#0000ff]End[/color][/size][size=2][color=#0000ff]Sub
 
End[/color][/size][size=2][color=#0000ff]Class
 
[/color][/size]
 
Top Bottom