FTP file transfers require two different sockets. The control socket is the socket on which you receive status information (in numerics) and send commands. The data socket(s) is(are) where you send or receive data (such as the results of a LIST command or sending/receiving a file). Using the PASV (Passive) command requests a different port for each such transaction, as shown below, for you to open a new socket to; some FTP servers require PASV, others don't and you can just keep reopening a socket to port 20.
Here is a sample FTP session that connects to an FTP server, logs in as Hammer, switches to the www directory, uploads a file and then exits. This log was generated using WS_FTP Lite Edition and then cleaned up to exclude non-essentials (like multiple LISTs), sanitized, colorized and notes added to show what happens and where/when..
- Grey text is what your script will /echo out of its own accord: Version info, socket info, etc.[color:#840017]
- Maroon text is sent to you from the server. Numerics and changeable data are in bold. All of these lines are read in the on SOCKREAD event.
- Green text is send by you to the server. Command names are in bold; parameters are not.
WINSOCK.DLL: WinSock 2.0
WS_FTP LE 5.08 2000.01.13, Copyright © 1992-2000 Ipswitch, Inc.
--> local chdir to C:\FTP
--> connecting to 12.34.56.78:21 [sockopen FTP 12.34.56.78 21]
--> Connected to 12.34.56.78 port 21 [on *:SOCKOPEN:FTP:]
220 ftp.account.net FTP server (Version 6.00) ready. [on *:SOCKREAD:FTP:]
USER Hammer [sockwrite -n FTP USER username]
331 Password required for Hammer.PASS dIdYoUrEaLlYtHiNkIdTeLlYoUmYpAsSwOrD [sockwrite -n FTP PASS password]
230 User Hammer logged in.CWD /home/Hammer/www/ [sockwrite -n FTP CWD /home/username/www/]
250 CWD command successful.--> sending regex.html as regex.html (1 of 1)
TYPE I [sockwrite -n FTP TYPE I]
200 Type set to I.PASV [sockwrite -n FTP PASV]
227 Entering Passive Mode (12,34,56,78,192,6)***; [192 * 256 + 6 = 49158] so /sockopen FTPData 12.34.56.78 49158
--> connecting to 12.34.56.78:49158
--> Connected to 12.34.56.78 port 49158 [on *:SOCKOPEN:FTPData:]
STOR regex.html [sockwrite -n FTP STOR filename.txt]
150[/b] Opening BINARY mode data connection for 'regex.html'.*** ; Use 8192-byte, 4096-byte, 2048-byte or 1024-byte chunks to fill up the send queue.
*** ; BRead C:\mIRC\FTP\filename.txt into a &binvar
*** ; SockWrite FTPdata &binvar
*** ; Check to see if the buffer is full or if there's room for more ($sock(FTP).sq tells you if you have more room)
*** ; Continue till buffer is full
*** ; Use on *:SOCKWRITE:FTPData: to retrigger the BRead/SockWrite process (if necessary)
*** ; Repeat until file is sent and $sock(FTPData).sq is 0.
*** ; Then /sockclose FTPData
--> Transmitted 10060 bytes in 0.1 secs, (98.24 KBps), transfer succeeded
226 Transfer complete.QUIT [sockwrite -n FTP QUIT]
221 Goodbye.[/b]*** ; on *:SOCKCLOSE:FTP: happens when the server closes the socket.[/color]
For more information, please see
RFC 959 - File Transfer Protocol (FTP).