create FTP Client

Bringing the internet highway into your project? Building FTP, HTTP, email, chat or other client solutions?

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
Dimaxter
Posts: 6
Joined: Wed Apr 01, 2009 7:43 pm
Contact:

create FTP Client

Post by Dimaxter » Wed Apr 01, 2009 8:11 pm

Please HELP! Problem "425 Unable to build data connection: Connection refused"

Code: Select all

on mouseUp
   put hostnameToAddress("www.site.com") into FTP
   put ":21" after FTP
   open socket to FTP
   get result()
   put it after field "connectInfo"
   
   write "USER mylogin" &CRLF to socket FTP
   read from socket FTP until CRLF
   put it after field "connectInfo"
   
   write "PASS xxxxxx" &CRLF to socket FTP
   read from socket FTP until CRLF
   put it after field "connectInfo"
   
   write "SYST" &CRLF to socket FTP
   read from socket FTP until CRLF 
   put it after fld "connectInfo"
   
   write "CWD /my/directory/" &CRLF to socket FTP
   read from socket FTP until CRLF 
   put it after fld "connectInfo"
   
   write "TYPE A" &CRLF to socket FTP
   read from socket FTP until CRLF 
   put it after fld "connectInfo"
   
   write "PORT 192,168,1,15,40,101" &CRLF to socket FTP
   read from socket FTP until CRLF 
   put it  after fld "connectInfo"
   
   write "LIST" &CRLF to socket FTP
   read from socket FTP until CRLF
   put it after fld "connectInfo"
   
   write "QUIT" &CRLF to socket FTP
   read from socket FTP until CRLF
   put it after fld "connectInfo"
end mouseUp
write "LIST" returns an error


Here's a response from the server:
  • 220 ProFTPD 1.3.1 Server ready.
    331 Password required for mylogin
    230 User mylogin logged in
    215 UNIX Type: L8
    250 CWD command successful
    200 Type set to A
    200 PORT command successful
    425 Unable to build data connection: Connection refused
Please help!!!

Mark
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 5150
Joined: Thu Feb 23, 2006 9:24 pm
Contact:

Post by Mark » Thu Apr 02, 2009 12:50 am

Hi Dimaxter,

Perhaps you should try sending PASV first.

Btw I created a simple FTP client without figuring out the FTP protocol, by using Rev's built-in libUrl commands.

Best,

Mark
The biggest LiveCode group on Facebook: https://www.facebook.com/groups/livecode.developers
The book "Programming LiveCode for the Real Beginner"! Get it here! http://tinyurl.com/book-livecode

Dimaxter
Posts: 6
Joined: Wed Apr 01, 2009 7:43 pm
Contact:

Post by Dimaxter » Thu Apr 02, 2009 2:13 pm

Mark wrote:Hi Dimaxter,

Perhaps you should try sending PASV first.

Btw I created a simple FTP client without figuring out the FTP protocol, by using Rev's built-in libUrl commands.

Best,

Mark
With the Rev's built-in libUrl commands I tried - is not enough! You need to create a directory on the server and update files.

Mark
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 5150
Joined: Thu Feb 23, 2006 9:24 pm
Contact:

Post by Mark » Thu Apr 02, 2009 2:19 pm

Hi Dimaxter,

You can do that within the built-in commands and whenever you get stuck you can use the lubUrlFtpCommand command.

Best,

Mark
The biggest LiveCode group on Facebook: https://www.facebook.com/groups/livecode.developers
The book "Programming LiveCode for the Real Beginner"! Get it here! http://tinyurl.com/book-livecode

Dimaxter
Posts: 6
Joined: Wed Apr 01, 2009 7:43 pm
Contact:

Post by Dimaxter » Thu Apr 02, 2009 5:56 pm

We need to continuously update the server files and folders are on the local computer in the selected folder

Dimaxter
Posts: 6
Joined: Wed Apr 01, 2009 7:43 pm
Contact:

Post by Dimaxter » Fri Apr 03, 2009 1:50 am

Mark wrote:Hi Dimaxter,

You can do that within the built-in commands and whenever you get stuck you can use the lubUrlFtpCommand command.

Best,

Mark

Code: Select all

 get libURLftpCommand ("LIST",FTP&":21",user,passwd)
 put it 

All the same error:

425 Unable to build data connection: Invalid argument

Dimaxter
Posts: 6
Joined: Wed Apr 01, 2009 7:43 pm
Contact:

Post by Dimaxter » Fri Apr 03, 2009 3:27 pm

Resolved:

Code: Select all

write "PASV" &CRLF to socket FTP
read from socket FTP until CRLF
put it after fld "connectInfo"
   
#ENCODE IP & PORT
   put empty into SIP
   put offset ("(", it) into st
   put offset (")", it) into en
   put char st+1 to en-1 of it into pasvIP
   set itemdel to ","
   repeat for each item X in pasvIP
      add 1 to Z
      if Z< 5 then
         put X &"." after SIP
      else if Z=5 then
         put (X*256) into XJ
      else if Z=6 then
         put ":"&(XJ + X) into prt
   end if       
   end repeat

delete char -1 of SIP
put SIP&prt into SIP

open socket to SIP
put openSockets()&LF after fld "connectInfo"

write "STOR avator.png" &CRLF to socket FTP
read from socket FTP until CRLF
put it after fld "connectInfo"

write URL ("binfile:avator.png")  &CRLF.CRLF to socket SIP

read from socket FTP until CRLF
put it after fld "connectInfo"

All power is "SIP"

X
Posts: 18
Joined: Mon Jan 05, 2009 12:20 pm

Understanding FTP Protocol

Post by X » Sun Jun 21, 2009 5:11 pm

Actually its all down to understanding the nature of FTP protocol.
FTP, unlike HTTP uses two channels to communicate and exchange data.

1. First channel (usually on port 21) is called 'Control Channel' (PI - Protocol Interpreter).
You use it to pass FTP commands to the server and server will respond to you on that channel as well.

2. Second channel (usually port 20) is called 'Data Channel' (DTP - Data Transmission Process).
This channel is for sending/receiving (uploading/downloading) content (your files, directory listings).

So in short, you talk to server on one channel but transfer files on other.

In Rev, as you may already guessed, you'll need to open and maintain communication on TWO sockets.
Now, first socket is easy and sraightforward:

open socket to "myhost:21"

But opening second, the Data socket, is a bit tricky.
This is where you learn another important FTP feature - the Passive Mode.
In most cases you will need this mode enabled.
You pass PASV command to the server on the Control Channel and server
will respond with confirmation like this:

227 Entering Passive Mode (123,123,123,123,111,222)

In the brackets is ASCII (32+16 bit) representation of host IP address and port number.

Item 1 to 4 of it is host IP address and last two items represent the port number, so logically it looks like this:

[123,123,123,123],[111,222]

With Passive Mode, you must connect on that port for Data Transmission.
You will open socket on that port to upload and download files.

It is easy to convert host IP part to a format we use simply be replacing commas with periods:

replace "," with "." in hostIP -->> 123.123.123.123

Now the crucial part - translating 16 bit (2x8) of ASCII port representation into single 16 bit port number.

[111,222] is two items, two (ASCII, base 10) groups of digits. Each group is in range 0...255 (2^8).

We need to convert it to single 16 bit format by combining these two,
but because they are in ASCII (base 10) representation, we first must convert them to either binary or hex format, then combine:
baseConvert ( part1, 10, 16) & baseConvert ( part2, 10, 16)

so the final code may look like this:

--// assuming server response on 'PASV' command you put into response var //--

if matchText(response,"\([0-9]*,[0-9]*,[0-9]*,[0-9]*,([0-9]*),([0-9]*)\)",p1,p2) then
get baseConvert(p1,10,16) & baseConvert(p2,10,16)
put baseConvert(it,16,10) into dataPort
put myHostName & ":" & dataPort into dataChannel
--// Open Data Port //--
open socket dataChannel
end if

Note the use of Perl alike Wildcard expression to find and extract two parts of the port number from the server response string.

So now you should be set and ready to talk to your FTP server and exchange data the proper way, far beyond revlibFTP and libURL limits all together ;-)

VX.

dablock
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 23
Joined: Wed Apr 23, 2008 2:51 pm

Post by dablock » Fri Aug 14, 2009 3:47 pm

X, that's really helpful info. I've also built a FTP transfer utility using rev's built-in commands & it works very well. But it's interesting to know what's going on under the hood.

Thanks for the info!

Post Reply