您的位置:首页 > 编程语言 > PHP开发

A Brief description of the FTP protocol

2004-12-01 17:10 351 查看
The FTP protocol require two programs, a server program, and a client program. Normally the server program offer files to the client program. But in some cases, the server will also allow the client to upload files.

A simple FTP session
Server: 220 Sample FTP server ready. Please give user-name
Client: USER anonymous
Server: 331 User name OK. Please give your email address as password
Client: PASS joe@nowhere.comm
Server: 230 User logged in
As you can see, the client and server are communicating in plain text. The digits in the server replies are 'relpy-codes' defined by the FTP protocol. The uppercase words in the beginning of the client commands, are command verbs that also are defined by the RFC. The protocol is designed in a way that makes it easy for machines and humans to understand the dialog. In most cases, the client program don't have to interperate the text after the reply code.
Now, the user want's to see the available files and dirs, and issues a DIR command in the client program. Client: TYPE A
Server: 200 Type set to A
Client: PASV
Server: 227 Entering passive mode (193,91,161,12,28,46)
Client: LIST
Server: 150 Opening ASCII mode data connection for /bin/ls
Server: 226 Transfer complete
The TYPE command tells the server to send the directoty/file listing as plain ASCII, where each line is seperated by a CRLF sequence.
The PASV command tells the server to prepere for a new socket connection by creating a new socket and listen for a connection from the client. And now, as you see, things are getting a little more complicated. The server reply includes a IP address and a port number, encoded as 6 digits, seperated ny commas. The client must find and understand this address in order to receive the listing.
The LIST command tells the server to give a directory/file listing. And now the server replies with two reply lines... What is happening is that the firts line tells the client that the listing is ready, and that the client can go on and make a new connection to the server. The client connects to the IP address given by the PASV reply, and receives data until there is no more data to get. Then it close the temporary data connaction and switch back to the control connection to get's the second reply line, which tells if the server transfered the whole listing.
In order to receive a directory listing, the client and server use two socket connections, one for the control flow (server sends commands, the server replies in plain text) and one for the data connection (which is continious and goes in one direction only). Next time a directory-listing is sent, the server and client will use another new (temporaty) socket connection for the transfer.
The user finds an interesting file, and give the FTP client the command to get it. Client: TYPE I
Server: 200 Type set to I
Client: PASV
Server: 227 Entering passive mode (193,91,161,12,28,46)
Client: RETR test.zip
Server: 150 Opening BINARY mode data connection for test.zip
Server: 226 Transfer complete
As you see, the server and client use the exact same method to get a file, as they do to get a directory listing. The only change is that the RETR command is used in stead of the LIST command. In this case the file was a .zip archive, and since such files can't be translated to text-files (at least not in a safe manner), the FTP client switched to binary mode (TYPE I). Files and directory listings can be transfered in both binary and text-mode. Since different operating systems use different means to seperate lines in text-files, it is generally a good idea to let the FTP server and FTP client do the conversion. Both know the rules for a ASCII (text) transfer, and the client always knows the rules for the local storage of text-files. You can easily connect to a FTP server with a Telnet client and give commands, but due to the fact that the file-trasfers use a seperate socket connection, you can not (easily) transfer files without a FTP client.

                                                                                          By Jarle Aase
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息