Posted: Thu Nov 21, 2002 10:27 pm
Restored from previous forum. Originally posted by TerryHough.
Does anyone have any working FTP client code?
TIA
Terry
Does anyone have any working FTP client code?
TIA
Terry
http://www.purebasic.com
https://www.purebasic.fr/english/
Code: Select all
;
; Designed by FRW 20020603
; Updates 20021122
;
; WRP_Nasca_FTP-t is a test ftp client for testing and training purpose.
;
; It is designed from original WRP_Nasca_FTP with a reduced user interface template.
;
; See in line comments for any needed help.
;
#MAX_PATH = 260
#FILE_ATTRIBUTE_READONLY = 1
#FILE_ATTRIBUTE_HIDDEN = 2
#FILE_ATTRIBUTE_SYSTEM = 4
#FILE_ATTRIBUTE_DIRECTORY = 16
#FILE_ATTRIBUTE_ARCHIVE = 32
#FILE_ATTRIBUTE_NORMAL = 128
#FILE_ATTRIBUTE_TEMPORARY = 256
#FILE_ATTRIBUTE_COMPRESSED = 2048
#NO_ERROR = 0
#ERROR_NO_MORE_FILES = 18
#ERROR_INTERNET_EXTENDED_ERROR = 12003
#FTP_TRANSFER_TYPE_BINARY = 2
#INTERNET_OPEN_TYPE_DIRECT = 1
#INTERNET_DEFAULT_FTP_PORT = 21
#INTERNET_SERVICE_FTP = 1
#Main_Window = 92
#Gadget_String_Input = 11
#Gadget_OK_Button = 12
#Gadget_Quit_Button = 13
#Gadget_Text_Output = 14
#Gadget_Help = 15
Structure PB_FIND_DATA
FileAttributes.l
CreationTimeL.l
CreationTimeH.l
LastAccessTimeL.l
LastAccessTimeH.l
LastWriteTimeL.l
LastWriteTimeH.l
FileSizeHigh.l
FileSizeLow.l
OID.l
FileName.s[#MAX_PATH]
EndStructure
DefType.WIN32_FIND_DATA PB_FIND_DATA
hInternetSession.l
hInternetConnect.l
hMainWindow.l
UsedFont.s
UsedFontSize.s
EOL.s
*PathBuff = AllocateMemory(0, 2500, 0)
Global EOL, UsedFont, UsedFontSize, hInternetSession, hInternetConnect, hMainWindow, PB_FIND_DATA
;
; Procedures
;
Procedure MyWindowCallback(WindowID, Message, wParam, lParam)
Result = #PB_ProcessPureBasicEvents
Select Message
Case #WM_SIZE
WindowXSize = WindowWidth()
WindowYSize = WindowHeight()
ResizeGadget(#Gadget_String_Input, 10, 10, WindowXSize - 20, 20)
ResizeGadget(#Gadget_Text_Output, 10, 70, WindowXSize - 20, WindowYSize - 90)
ResizeGadget(#Gadget_OK_Button, WindowXSize - 110, 40, 40, 20)
ResizeGadget(#Gadget_Quit_Button, WindowXSize - 60, 40, 40, 20)
EndSelect
ProcedureReturn Result
EndProcedure
;
; Main starts here
;
BytesRead.w
BytesToRead.w
hGetFile.l
hCurrentDirectory.l
hResponse.l
i.l
iu.l
WindowXSize.l
WindowYSize.l
ScreenWidth.l
ScreenHeight.l
WindowXstart.l
WindowYStart.l
FontPointer.l
LSFileAttributes.l
LSCreationTimeL.l
LSCreationTimeH.l
LSLastAccessTimeL.l
LSLastAccessTimeH.l
LSLastWriteTimeL.l
LSLastWriteTimeH.l
LSFileSizeHigh.l
LSFileSizeLow.l
LSOID.l
LShFindFile.l
NItems.l
Name.s
UserName.s
Password.s
Path.s
SubPath.s
File.s
File1.s
File2.s
LSFileName.s
LSTempFileName.s
LSTempFA.s
LSTempFA1.s
LSTempFA2.s
LSTempFA3.s
LSTempFA4.s
LSTempFA5.s
LSTempFA6.s
LSTempFA7.s
LSTempFA8.s
LSTempFileSize.s
ServiceMessage.s
InternetReadFileName.s
FTPUserCommand.s
FTPUserArgs.s
CommandLine.s
SBuff.s
ServiceMessage.s
CharacterSet.s
ServiceMessage.s
;
; General purpose initalization
;
EOL = Chr(13) + Chr(10)
ServiceMessage.s = ""
Name.s = ""
UserName.s = ""
Password.s = ""
SubPath.s = ""
FTPUserCommand.s = ""
FTPUserArgs.s = ""
CommandLine.s = ""
BytesToRead = 10000
ServiceMessage.s = ""
Quit = #FALSE
WindowXSize = 480
WindowYSize = 360
WindowXStart = (GetSystemMetrics_(#SM_CXSCREEN) - WindowXSize) / 2
WindowYStart = (GetSystemMetrics_(#SM_CYSCREEN) - WindowYSize) / 2
FTPSession = 0
;
; Create and use the Main window
;
hMainWindow = OpenWindow(#Main_Window, WindowXStart, WindowYStart, WindowXSize, WindowYSize, #PB_Window_SystemMenu | #PB_Window_TitleBar | #PB_Window_BorderLess | #PB_Window_MinimizeGadget | #PB_Window_MaximizeGadget | #PB_Window_SizeGadget, "WRP - Nasca FTP Client - Main Window")
If hMainWindow
AddKeyboardShortcut(#Main_Window, #PB_Shortcut_Return, #Gadget_OK_Button)
StartDrawing(WindowOutput())
FrontColor(255, 255, 180)
BackColor(40, 40, 80)
UsedFont = "Courier New"
UsedFontSize = "8"
Result = LoadFont(0, UsedFont, Val(UsedFontSize))
UseFont(0)
FontID = FontID()
Result = CreateGadgetList(WindowID())
SetGadgetFont(FontID())
StringGadget(#Gadget_String_Input, 10, 10, WindowXSize - 20, 20, "") ; command input string box
StringGadget (#Gadget_Text_Output, 10, 70, WindowXSize - 20, WindowYSize - 90, "open a connection first using open server user:pass", #PB_String_Multiline | #ES_AUTOVSCROLL | #WS_VSCROLL | #WS_HSCROLL) ; display output text box
DrawingFont(FontID)
ButtonGadget (#Gadget_OK_Button, WindowXSize - 110, 40, 40, 20, "OK")
ButtonGadget (#Gadget_Quit_Button, WindowXSize - 60, 40, 40, 20, "Quit")
StopDrawing()
SetWindowCallback(@MyWindowCallBack())
;
; Preparing the drawing window context
;
Result = CreateGadgetList(WindowID())
Repeat
ServiceMessage = ""
WEvent = WaitWindowEvent()
WIDEvent = EventWindowID()
GEvent = EventGadgetID()
;
; 3 ways to stop and quit : Close the window / Key in ESC key / key in quit command in command line (see below)
;
If WEvent = #PB_EventCloseWindow And WIDEvent = #Main_Window
Quit = #TRUE
EndIf
;
; Keys
;
If EventType() = #PB_EventType_ReturnKey ; Return key has been pushed
CommandLine = GetGadgetText(#Gadget_String_Input) + " "
iu = FindString(CommandLine, " ", 1)
If iu = 0
FTPUserCommand = CommandLine
FTPUserArgs = ""
Else
FTPUserCommand = Mid(LCase(LTrim(RTrim(CommandLine))), 1, iu -1)
FTPUserArgs = LTrim(RTrim(Mid(CommandLine, iu, Len(CommandLine) - iu + 1)))
EndIf
SetGadgetText(#Gadget_String_Input, "")
EndIf
;
; Gadget events
;
Select GEvent ; If Gadgets were activated
Case #Gadget_OK_Button ; OK button
CommandLine = GetGadgetText(#Gadget_String_Input) + " "
iu = FindString(CommandLine, " ", 1)
If iu = 0
FTPUserCommand = CommandLine
FTPUserArgs = ""
Else
FTPUserCommand = LCase(LTrim(RTrim(Mid(CommandLine, 1, iu -1))))
FTPUserArgs = LTrim(RTrim(Mid(CommandLine, iu, Len(CommandLine) - iu + 1)))
EndIf
SetGadgetText(#Gadget_String_Input, "")
Case #Gadget_Quit_Button ; Quit button
Quit = #TRUE
Default
EndSelect
If FTPSession = 1
;
; Following commands are only possible when an FTP session exists
;
; cd change remote working directory
; delete file delete remote file
; dir [path] list remote directory
; get file download file from server to client
; ls [path] list remote directory
; mkdir [path] create a new remote directory
; put file upload file from client to server
; pwd print remote working directory
; recv file download file from server to client
; rename file1 file2 rename remote file1 into file2
; rmdir [path] remove remote directory
; send file upload file from client to server
;
;
; If FTPUserCommand was generated through events
;
If FTPUserCommand ""
ServiceMessage = FTPUserCommand + " " + FTPUserArgs ; Default ServiceMessage
Select FTPUserCommand
Case "cd" ; change working directory on server
If FtpSetCurrentDirectory_(hInternetConnect, @FTPUserArgs)
Else
ServiceMessage = "FTPSetCurrentDirectory_ error : " + Str(GetLastError_())
EndIf
Case "delete" ; delete file
If FtpDeleteFile_(hInternetConnect, FTPUserArgs)
Else
ServiceMessage = "FTPDeletFile_ error : " + Str(GetLastError_())
EndIf
Case "dir" ; dir = ls
Goto ls_command
get_command: Case "get" ; get file from server
If FtpGetFile_(hInternetConnect, FTPUserArgs, FTPUserArgs, 0, #FILE_ATTRIBUTE_NORMAL, #FTP_TRANSFER_TYPE_BINARY, 0)
Else
ServiceMessage = "FTPGetFile_ error : " + Str(GetLastError_())
EndIf
Case "help"
Goto help_command
ls_command: Case "ls" ; ls edit the content of the current directory
NItems = 0
SetGadgetText(#Gadget_Text_Output, "")
LSFileName = FTPUserArgs
;
; The process consists in FindFirstFile to fetch the first item and a loop
; with FindNextFile.
;
; Each item has parameters stored in WIN32_FIND_DATA accessible with
; PB_FIND_DATA by structure equivalence. As far as items are found the loop
; continues until a ERROR_NO_MORE_FILES error code. It is then necessary
; to close the handle opened with FindFirstFile.
;
ServiceMessage = ""
ServiceMessage = ""
LShFindFile = FtpFindFirstFile_(hInternetConnect, LSFileName + "*", @PB_FIND_DATA, 0, 0)
If LShFindFile
NItems = NItems + 1
While GetLastError_() #ERROR_NO_MORE_FILES And GetLastError_() #ERROR_FILE_NOT_FOUND
;
; Loads the stored file information in local specific variables
;
LSFileAttributes = PeekL( @PB_FIND_DATA )
LSCreationTimeL = PeekL( @PB_FIND_DATA + 4 )
LSCreationTimeH = PeekL( @PB_FIND_DATA + 8 )
LSLastAccessTimeL = PeekL( @PB_FIND_DATA + 12 )
LSLastAccessTimeH = PeekL( @PB_FIND_DATA + 16 )
LSLastWriteTimeL = PeekL( @PB_FIND_DATA + 20 )
LSLastWriteTimeH = PeekL( @PB_FIND_DATA + 24 )
LSFileSizeHigh = PeekL( @PB_FIND_DATA + 28 )
LSFileSizeLow = PeekL( @PB_FIND_DATA + 32 )
LSOID = PeekL( @PB_FIND_DATA + 36 )
LSFileName = PeekS( @PB_FIND_DATA + 44 )
;
; Generates a 40 characters file name just for the listing
;
LSTempFileName = LSFileName
If Len(LSTempFileName) 18
ServiceMessage = "ls error" + EOL + Str(GetLastError_())
EndIf
EndIf
Case "mkdir" ; mkdir create a new directory entry point in the current working directory
If FtpCreateDirectory_(hInternetConnect, FTPUserArgs)
Else
ServiceMessage = "FTPCreateDirectory_ error : " + Str(GetLastError_())
EndIf
Case "put" ; put file to server
put_command: If FtpPutFile_(hInternetConnect, FTPUserArgs, FTPUserArgs, 0, 0)
Else
ServiceMessage = "FTPPutFile_ error : " + Str(GetLastError_())
EndIf
Case"pwd" ; print the current server working directory
Length = #MAX_PATH
If FtpGetCurrentDirectory_(hInternetConnect, *PathBuff, @Length)
Path = PeekS(*PathBuff)
ServiceMessage = "pwd " + Path
Else
ServiceMessage = "FTPGetCurrentDirectory_ error : " + Str(GetLastError_())
EndIf
Case "recv" ; get file from server = get
Goto get_command
Case "rename" ; rename file1 into file2
iu = FindString(FTPUserArgs, " ", 1)
File1 = LTrim(RTrim(Mid(FTPUserArgs, 1, iu -1)))
File2 = LTrim(RTrim(Mid(FTPUserArgs, iu + 1, Len(FTPUserArgs) - iu - 1 + 1)))
If FtpRenameFile_(hInternetConnect, File1, File2)
ServiceMessage = "rename " + File1 + " " + File2
Else
ServiceMessage = "FTPRenameFile_ error : " + Str(GetLastError_())
EndIf
Case "rmdir" ; rmdir delete a empty directory entry point in the current working directory
If FtpRemoveDirectory_(hInternetConnect, FTPUserArgs)
Else
ServiceMessage = "FTPRemoveDirectory_ error : " + Str(GetLastError_())
EndIf
Case "send" ; put file to server = put
Goto put_command
Default
EndSelect
;
; I put this delay because I experienced much stability problems without ! Don't know why
;
FTPUserCommand = ""
FTPUserArgs = ""
EndIf
Else
;
; Some commands are allowed if no connection exists
; This is especially the case for giving connection parameters !
;
; Enabled commands at this level are :
;
; *** top level, connection and session parameters and commands ***
;
; open name [user[:password]] open a FTP session to server name with optional user and password account parameters
; quit close the FTP session and quit the program
;
; *** other management commands
;
; help
; show
;
Select FTPUserCommand
Case "open"
iu1 = FindString(FTPUserArgs, " ", 1)
iu2 = FindString(FTPUserArgs, ":", 1)
If iu1 0
If iu2 0
Name = Mid(FTPUserArgs, 1, iu1 - 1)
UserName = Mid(FTPUserArgs, iu1 + 1, iu2 - iu1 -1)
Password = Mid(FTPUserArgs, iu2 + 1, Len(FTPUserArgs) - iu2 - 1 + 1)
Else
Name = Mid(FTPUserArgs, 1, iU1 - 1)
UserName = Mid(FTPUserArgs, iu1 + 1 , Len(FTPUserArgs) - iu1 - 1 + 1)
EndIf
Else
Name = FTPUserArgs
EndIf
hInternetSession.l = InternetOpen_("FTP", #INTERNET_OPEN_TYPE_DIRECT, "", "", 0)
If hInternetSession 0
ServiceMessage = "Opened network" + EOL
;
; Connects to an FTP server
;
hInternetConnect.l = InternetConnect_(hInternetSession.l, Name, #INTERNET_DEFAULT_FTP_PORT, UserName, Password, #INTERNET_SERVICE_FTP, 0, 0)
If hInternetConnect 0
ServiceMessage = ServiceMessage + "Opened session on " + Name + " " + Username + ":" + Password + EOL
FTPSession = 1
Else
ServiceMessage = "InternetConnect_ failed"
EndIf
Else
ServiceMessage = "InternetOpen_ failed"
EndIf
Case "quit" ; quit closes the session and the program
If hInternetSession
If hInternetConnect
InternetCloseHandle_(hInternetConnect)
hInternetConnect = #FALSE
Else
ServiceMessage = "No connection to close"
EndIf
InternetCloseHandle_(hInternetSession)
hInternetSession = #FALSE
Else
ServiceMessage = "No Internet access to close"
EndIf
FTPSession = 0
Quit = #TRUE
Case "help"
;
; Prepare the Help window context
;
help_command: ServiceMessage = ""
Select FTPUserArgs
Case ""
ServiceMessage = ServiceMessage + "To start you must open a connection using :" + EOL + EOL
ServiceMessage = ServiceMessage + "open Server Username:password" + EOL + EOL
ServiceMessage = ServiceMessage + ". bye => quit" + EOL
ServiceMessage = ServiceMessage + ". cd path change directory" + EOL
ServiceMessage = ServiceMessage + ". debug set / unset internal Debug" + EOL
ServiceMessage = ServiceMessage + ". delete file delete a file" + EOL
ServiceMessage = ServiceMessage + ". dir path/file => ls" + EOL
ServiceMessage = ServiceMessage + ". get file download a file from server to client" + EOL
ServiceMessage = ServiceMessage + ". help display this information" + EOL
ServiceMessage = ServiceMessage + ". ls path/file list a directory/file (accepts generic characters)" + EOL
ServiceMessage = ServiceMessage + ". quit end of session" + EOL
ServiceMessage = ServiceMessage + ". put file upload a file from client to server" + EOL
ServiceMessage = ServiceMessage + ". recv => get" + EOL
ServiceMessage = ServiceMessage + ". rename file1 file2 change the name file1 into file2" + EOL
ServiceMessage = ServiceMessage + ". rmdir path remove a directory" + EOL
ServiceMessage = ServiceMessage + ". send => put"
Case "bye"
ServiceMessage = ServiceMessage + "bye : close the ftp session" + EOL + EOL
ServiceMessage = ServiceMessage + "bye is equivalent to quit" + EOL
Case "cd"
ServiceMessage = ServiceMessage + "cd path : change the current working directory on the server" + EOL
Case "debug"
ServiceMessage = ServiceMessage + "debug : opens a text console to display more processing details" + EOL
Case "delete"
ServiceMessage = ServiceMessage + "delete file : removes the named file from the server" + EOL
Case "dir"
ServiceMessage = ServiceMessage + "dir [path/filemask] : list the content of [path/filemask]" + EOL + EOL
ServiceMessage = ServiceMessage + "[path/filemask] can be any path/file access including meta characters" + EOL
ServiceMessage = ServiceMessage + "dir is equivalent to ls" + EOL
Case "get"
ServiceMessage = ServiceMessage + "get file : transmit file from server to client" + EOL
Case "help"
ServiceMessage = ServiceMessage + "help [command] : help gives the general help information." + EOL + EOL
ServiceMessage = ServiceMessage + "Giving a command name in argument gives a more detailed help" + EOL
ServiceMessage = ServiceMessage + "for the given command" + EOL
Case "ls"
ServiceMessage = ServiceMessage + "ls [path/filemask] : list the content of [path/filemask]" + EOL + EOL
ServiceMessage = ServiceMessage + "[path/filemask] can be any path/file access including meta characters" + EOL
Case "quit"
ServiceMessage = ServiceMessage + "quit : close the ftp session" + EOL
Case "put"
ServiceMessage = ServiceMessage + "put file : transmit file from client to server" + EOL
Case "recv"
ServiceMessage = ServiceMessage + "recv file : transmit file from server to client" + EOL + EOL
ServiceMessage = ServiceMessage + "recv is equivalent to get" + EOL
Case "rename"
ServiceMessage = ServiceMessage + "rename file1 file2 : change filename file1 to file2" + EOL
Case "rmdir"
ServiceMessage = ServiceMessage + "rmdir directory : remove directory" + EOL
Case "send"
ServiceMessage = ServiceMessage + "send file : transmit file from client to server" + EOL + EOL
ServiceMessage = ServiceMessage + "send is equivalent to put" + EOL
Default
ServiceMessage = ServiceMessage + "Check functionalities using : help" + EOL
EndSelect
FTPUserCommand = ""
FTPUserArgs = ""
SetGadgetText(#Gadget_Text_Output, ServiceMessage)
Case "show"
ServiceMessage = ""
ServiceMessage = ""
ServiceMessage = ServiceMessage + "Server : " + Name + EOL
ServiceMessage = ServiceMessage + "Username : " + UserName + EOL
ServiceMessage = ServiceMessage + "Password : " + Password + EOL
ServiceMessage = ServiceMessage + EOL
ServiceMessage = ServiceMessage + "FTPSession : " + Str(FTPSession) + EOL
ServiceMessage = ServiceMessage + "hInternetSession : " + Str(hInternetSession) + EOL
ServiceMessage = ServiceMessage + "hInternetConnect : " + Str(hInternetConnect) + EOL
ServiceMessage = ServiceMessage + EOL
ServiceMessage = ServiceMessage + "Font : " + UsedFont + EOL
ServiceMessage = ServiceMessage + "Font size : " + UsedFontSize + EOL
SetGadgetText(#Gadget_Text_Output, ServiceMessage)
Default
EndSelect
EndIf
If ServiceMessage ""
SetGadgetText(#Gadget_Text_Output, ServiceMessage)
EndIf
Until Quit
Else
EndIf ; Close hMainWindow
If InternalDebug > 0
CloseConsole()
EndIf
If hInternetConnect
InternetCloseHandle_(hInternetConnect)
EndIf
If hInternetSession
InternetCloseHandle_(hInternetSession)
EndIf
End
Thanks for your response and I will study the code you posted. IOriginally posted by fweil
BTW, I place here a sample code I made and cannot find again in tricks'n tips.
It is not perfect but may help you to start.
Francois Weil
Found the answer and it is shown in postingOriginally posted by TerryHough
Thanks to fweil, I am nearing completion of the FTP program I needed.
His code example helped tremendously.
I am struggling with one problem.
When I attempt to connect to the Internet (and a current connection doesn't already exist), the program uses the DUN entry described as "default" (when set properly) and dials out and connects.
Here is my problem... When the program finishes and the disconnect command is issued, the internet (modem) connection is left active.
If my program created the connection, I need to tell the modem to hangup, but I don't know how to do that OR even how to tell if my program caused the modem to create the dialup connection.
Added later:
HELP! I'm still struggling with getting the modem to drop the connection. Any ideas appreciated.
@Mandy: WHAT? If I understand you right, your provider told you to signup here for creating your website!?!? If you need an FTP Client use FlashFXP or CuteFTP. With this you can upload your created HTML Website to the Providers Webserver.Originally posted by mandy
Okay this may be a stupid question but I am completely lost. I am trying to make my own website through my internet provider. However, they told me to come here and register so I can obtain an FTP client code. Okay so now I am registered. What is my FTP client code? I am confused.
Mandy
[url]mailto:mandy84@bayou.com">mandy84@bayou.com