FTP access

Just starting out? Need help? Post your questions and find answers here.
User avatar
kenmo
Addict
Addict
Posts: 2033
Joined: Tue Dec 23, 2003 3:54 am

FTP access

Post by kenmo »

I think I am missing something... I know there is an example of an FTP server, but what would you have to do to connect to ANOTHER ftp server through PureBasic? All I need is a simple user/password login and upload/download system for my program, but I havent found any examples on how to actually access another server... any help?
User avatar
Paul
PureBasic Expert
PureBasic Expert
Posts: 1282
Joined: Fri Apr 25, 2003 4:34 pm
Location: Canada
Contact:

Post by Paul »

Search is your friend :)

It might take you to something like this...
viewtopic.php?t=8692&highlight=ftp

which will also point you to...
http://www.reelmediaproductions.com/purebasic

(which also had a search at the top right of the page so just type ftp inthere also)

Enjoy !!


ps. Personally I find it easiest to just use Windows API commands for FTP but it is also quite simple to write your own in PureBasic.
Image Image
User avatar
kenmo
Addict
Addict
Posts: 2033
Joined: Tue Dec 23, 2003 3:54 am

Post by kenmo »

Ahh, how come search didnt find that for me??? Ill have to try again...

Thanks though, man.
Dare2
Moderator
Moderator
Posts: 3321
Joined: Sat Dec 27, 2003 3:55 am
Location: Great Southern Land

Post by Dare2 »

Heya, kenmo.

Not sure if I'm on the same wavelength, but:

If you're talking server to server then would an automated client on the one server do the job? Or automated file-sharing?

If you're looking for clients, found some on one of these two sites (links courtesy of freak):

http://www.purearea.net/ (download the code snippets zip)
http://www.reelmediaproductions.com/pb/

Maybe on both - I have just been downloading heaps of stuff. Also found something called FTPxchg on one of the sites, it looks pretty awesome in theory and shouldn't be hard to automate, although there is a prob with a structure so I can't say for sure.

And - If I missed the point completely, apologies!

(A real expert will be along shortly to sort things out.) :P


EDIT:

The expert was faster! See above! :?
User avatar
kenmo
Addict
Addict
Posts: 2033
Joined: Tue Dec 23, 2003 3:54 am

Post by kenmo »

Dare2 wrote:If you're talking server to server then would an automated client on the one server do the job?
Haha, well Im not sure what you mean either... Basically I want to edit an online file from PB and I figured the way to do it would be download it, edit it, and then upload it... probably using FTP commands. Imagine like a high score list, and if you get a high score you can add it to an online list... that kind of thing.
Dare2
Moderator
Moderator
Posts: 3321
Joined: Sat Dec 27, 2003 3:55 am
Location: Great Southern Land

Post by Dare2 »

Ah.

Could you just post (via http) to a special page on the server, and let some background scripting (via ASP or Perl, whatever) update the file?

Providing you have scripting access, that would be a darn site easier than writing an ftp client as a part of your game or app. PB has HTTP stuff to make it a few lines of code on the client side, script shouldn't be much more.

I am pretty sure I saw http examples somewhere in the downloads from the two sites above - can't recall exact files (I have a 1 kb brain handling gigs of new info, so much synaptic thrashing is going on and the index is failing ... )

Anyhow, have a good one.
User avatar
kenmo
Addict
Addict
Posts: 2033
Joined: Tue Dec 23, 2003 3:54 am

Post by kenmo »

Yeah, that would be easier... but I dont know any ASP or Perl...

I was hoping there would be some simple commands like

ftpconnect(server,user$,pass$)

you know? then in the game it would just be

ftpconnect
downloadfile
edit here....
uploadfile
closeftp or somethin...

Ohhh well.
Dare2
Moderator
Moderator
Posts: 3321
Joined: Sat Dec 27, 2003 3:55 am
Location: Great Southern Land

Post by Dare2 »

Okay, in the codesnippets zip file from http://www.purearea.net there is a folder called "internet&co".

(EDIT: Take the CodeArchive link)

That has heaps of stuff, including http downloads, ftp, etc. I bet there are some things in there that you can bend and use.

Cya.
Dare2
Moderator
Moderator
Posts: 3321
Joined: Sat Dec 27, 2003 3:55 am
Location: Great Southern Land

Post by Dare2 »

PS: What's the game, and when can we play it? :D
User avatar
Paul
PureBasic Expert
PureBasic Expert
Posts: 1282
Joined: Fri Apr 25, 2003 4:34 pm
Location: Canada
Contact:

Post by Paul »

Here is some simple code I wrote for FTP Access...

Code: Select all

Procedure.l FTPInit()
  ProcedureReturn InternetOpen_("FTP",1,"","",0)
EndProcedure


Procedure.l FTPConnect(hInternet,Server.s,User.s,Password.s,Port.l)
  ProcedureReturn InternetConnect_(hInternet,Server,Port,User,Password,1,0,0)
EndProcedure


Procedure.l FTPDir(hConnect.l)
  hFind=FtpFindFirstFile_(hConnect,"*.*",@FTPFile.WIN32_FIND_DATA,0,0)
  If hFind
    Find=1
    Debug PeekS(@FTPFile\cFileName)
    While Find
      Find=InternetFindNextFile_(hFind,@FTPFile)
      If Find
        Debug PeekS(@FTPFile\cFileName)
      EndIf      
    Wend
  EndIf
EndProcedure


Procedure.l FTPSetDir(hConnect.l,Dir.s)
  ProcedureReturn FtpSetCurrentDirectory_(hConnect,Dir)
EndProcedure


Procedure.l FTPCreateDir(hConnect.l,Dir.s)
  ProcedureReturn FTPCreateDirectory_(hConnect,Dir)
EndProcedure


Procedure.l FTPDownload(hConnect.l,Source.s,Dest.s)
  ProcedureReturn FTPGetFile_(hConnect,Source,Dest,0,0,0,0)
EndProcedure


Procedure.l FTPUpload(hConnect.l,Source.s,Dest.s)
  ProcedureReturn FTPPutFile_(hConnect,Source,Dest,0,0)
EndProcedure


Procedure.l FTPClose(hInternet.l)
  ProcedureReturn InternetCloseHandle_(hInternet)
EndProcedure






;--------------------
hInternet=FTPInit()
If hInternet
  hConnect=FTPConnect(hInternet,"ftp.reelmediaproductions.com","guest","guest",21)
  If hConnect
    
    FTPSetDir(hConnect,"test/")
    FTPDir(hConnect)
    FTPDownload(hConnect,"hello.txt","C:\download.txt")
    FTPUpload(hConnect,"C:\Download.txt","download.txt")

    FTPClose(hInternet)
  EndIf
EndIf
End
Basically wrappers for API commands but maybe the syntax makes a little more sense for beginners ;)
Image Image
Dare2
Moderator
Moderator
Posts: 3321
Joined: Sat Dec 27, 2003 3:55 am
Location: Great Southern Land

Post by Dare2 »

hehe - too fast! It works with my servers.

:)
Johan_Haegg
User
User
Posts: 60
Joined: Wed Apr 30, 2003 2:25 pm
Location: Västerås
Contact:

Post by Johan_Haegg »

Hm, i dont personaly like the FTPClient part of InternetExplorer, and i guess it uses that API :>

When developing client/server applications for existing protocols, please consult RFC

http://www.rfc-editor.org/

And its more fun to write your own without those APIs ;)
lazy
New User
New User
Posts: 7
Joined: Tue Nov 04, 2003 4:23 pm

Post by lazy »

Thanks a lot Paul, But how can i have the size of the files ?
And if you know how can i run the connection procedure in back task
And the last, How can i have the listing about the connection ?

Code: Select all

230 Anonymous user logged in.
331 Anonymous access allowed, send identity (e-mail name) as password.
220 ESM Microsoft FTP Service (Version 5.0).
thx ;)

(sorry for my english :D)
TerryHough
Enthusiast
Enthusiast
Posts: 781
Joined: Fri Apr 25, 2003 6:51 pm
Location: NC, USA
Contact:

Post by TerryHough »

lazy wrote:How can i have the size of the files ?
Here is how I do it when using the FTP API calls.

Code: Select all

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

;---------------------------------
      ; Get via FTP the first item from the Host's directory.
      ; Here, I want all the entries, but it could be restricted to a file
      ; type if desired, eg. "*.EXE" to get just the executable files.
      LShFindFile = FtpFindFirstFile_(File\hInternetConnect, "*.*", @PB_FIND_DATA, 0, 0)
      If LShFindFile
        ; There was at least one entry, so interpret it.
        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.s      = PeekS( @PB_FIND_DATA + 44 )
  
          If #FILE_ATTRIBUTE_DIRECTORY & LSFileAttributes
            ; It is a Directory entry, so ignore it 
          Else
            ; It is a File entry, so get all the info
            File\FileName      = Trim(LSFileName)  ; Get the file name
            File\Bytes         = (LSFileSizeLow + 1024 * LSFileSizeHigh) ; and size 
            File\FileSizeTotal = File\FileSizeTotal + File\Bytes ; Accum the file size
            File\FilesToGet    = File\FilesToGet + 1    ; Accum the file count
            ; Now, store the file name in our list (ListIconGadget)
            AddGadgetItem(42, -1, File\FileName+Chr(10)+Str(File\Bytes))
          EndIf  
          ; And, ask for the next entry in the directory
          InternetFindNextFile_(LShFindFile, @PB_FIND_DATA)
          ; until there are no more entries.
        Wend
        InternetCloseHandle_(LShFindFile)
      Else
        ; There were no files meeting the specs to be downloaded.
        FilesToGet = 0       
      EndIf   
      ; ----------------------------------------------------------------
      ; List is built
      ; ----------------------------------------------------------------
lazy wrote:And the last, How can i have the listing about the connection ?

Code: Select all

230 Anonymous user logged in.
331 Anonymous access allowed, send identity (e-mail name) as password.
220 ESM Microsoft FTP Service (Version 5.0).
AFAIK, the FTP APIs don't return this conversation. But you can easily do this using the FTP for all PB OS's thanks to Num3. See my example about this in your other post today.

Terry
User avatar
NoahPhense
Addict
Addict
Posts: 1999
Joined: Thu Oct 16, 2003 8:30 pm
Location: North Florida

Post by NoahPhense »

Paul... sweet... just what I was looking for..

thanks..

- np
Post Reply