A Free Trial That Lets You Build Big!
Start building with 50+ products and up to 12 months usage for Elastic Compute Service
FTP is the abbreviation for file Transfer Protocol (document Transfer Protocol), and Chinese is called "Interfax protocol". Used for two-way transmission of control files on the Internet. At the same time, it is also an application (application). There are different FTP applications based on different operating systems, and all of these applications follow the same protocol to transfer files. In the use of FTP, users often encounter two concepts: "Download" (Download) and "Upload" (Upload). A "Download" file is a copy of a file from a remote host to its own computer; the "upload" file is a copy of the file from your computer to a remote host. In the Internet language, users can upload (download) files to (from) a remote host through a client program.
Like most Internet services, FTP is also a client/server system. The user connects to the FTP server program on the remote host through a client program that supports the FTP protocol. The user sends commands to the server program through the client program, the server executes the commands issued by the user, and returns the results of the execution to the client. For example, a user sends a command asking the server to send a copy of a file to the user, and the server responds to the command, sending the specified file to the user's machine. The client program receives the file on behalf of the user and stores it in the user directory.
To connect to the FTP server (that is, "login"), you must have the FTP server authorized account, that is, you only have a user ID and a password to log on to the FTP server, to enjoy the services provided by the FTP server.
2) FTP address format
The FTP address is as follows: ftp://user name: password @ftp server IP or domain name: FTP command port/path/file name
The above parameters are not required except for the FTP server IP or domain name as necessary. The following addresses are valid FTP addresses:
When using FTP, you must first log in and obtain the appropriate permissions on the remote host before you can download or upload the file. That is, if you want to transfer files to the same computer, you must have the appropriate authorization for which computer. In other words, the file cannot be transferred unless there is a user ID and password. This situation violates the openness of the Internet, the FTP host on the internet is more than tens of millions, it is impossible to require each user to have an account on each host. Anonymous FTP was created to solve this problem.
Anonymous FTP is a mechanism by which a user can connect to a remote host and download files from it without becoming a registered user. The system administrator has established a special user ID, named Anonymous, that can be used anywhere on the Internet by anyone.
The FTP program connects anonymous FTP hosts in much the same way as a normal FTP host, except that anonymous must be entered when a user ID is required, and the password for that userid can be any string. In practice, use your own e-mail address as a password, so that the system maintenance program can record who is accessing these files.
It is important to note that anonymous FTP does not apply to all Internet hosts, it only applies to those hosts that provide the service.
When the remote host provides an anonymous FTP service, certain directories are specified to be open to the public, allowing anonymous access. The remaining directories in the system are in a stealth state. As a security measure, most anonymous FTP hosts allow users to download files from them without allowing users to upload files to them, which means that the user can copy all the files on the anonymous FTP host to their machine, but cannot copy any of the files on their machine to an anonymous FTP host. Even if some anonymous FTP hosts do allow users to upload files, users can only upload files to a specified upload directory. Then, the system administrator will check these files, he will move these files to another public download directory for other users to download, in this way, the remote host users are protected, to avoid someone uploading the problematic files, such as virus files.
By default, the FTP protocol uses the 20 and 21 ports in the TCP port, where 20 is used for transmitting data and 21 for transmitting control information. However, whether to use 20 as the port for transmitting data is related to the transfer mode used by FTP, if active mode is used, then the data transfer port is 20, and if passive mode is used, which port is used by the server side and the client to negotiate the decision.
There are two ways to transfer ftp: ASCII, Binary.
ASCII transmission mode
Assuming that the user is copying a file that contains simple ASCII text, if it is not UNIX running on the remote machine, FTP usually automatically adjusts the contents of the file when the file is transferred in order to interpret the file as the format in which the other computer stores the text file.
However, there are often cases where users are transmitting files that contain not text files, which may be programs, databases, word processing files, or compressed files. Before copying any non-text files, use the binary command to tell FTP verbatim copies.
Binary transfer Mode
In binary transmission, the order of the files is saved so that the original and copy are bit-wise. Even files that contain bit sequences on the destination machine are meaningless. For example, the Macintosh transmits the executable file to the Windows system in binary mode, and the file cannot be executed on the other system.
If the binary file is transmitted in ASCII mode, it will still be translated even if it is not required. This can damage the data. (ASCII generally assumes that the first significant bit of each character is meaningless, because the ASCII character combination does not use it.) If a binary file is transferred, all bits are important. ）
FTP supports two modes: standard (port mode, active mode), Passive (PASV, passive mode).
The FTP client first establishes a connection to the server's TCP 21 port, which is used to send a command that sends the port command on this channel when the client needs to receive data. The port command contains what ports the client uses to receive data. When transmitting data, the server connects to the client's specified port via its TCP 20 port to send the data. The FTP server must establish a new connection with the client to transfer the data.
Establish a control channel similar to standard mode, but send the PASV command after establishing a connection. After the server receives the PASV command, it opens a temporary port (with a port number greater than 1023 less than 65535) and notifies the client of the request to transmit data on this port, the client connects to this port on the FTP server, and the FTP server transmits the data through this port.
Many firewalls are not allowed to accept externally initiated connections when they are set up, so many FTP servers behind firewalls or intranet do not support PASV mode because clients cannot open the high-end port of the FTP server through the firewall, and many intranet clients cannot log on to the FTP server using port mode. Because TCP 20 from the server cannot establish a new connection to the internal network client, it does not work.
Active and Passive mode FTP has two modes of use: active and passive. Active mode requires the client and server to open and listen to a port at the same time to establish a connection. In this case, the client has some problems because the firewall is installed. Therefore, the passive mode was created. Passive mode only requires the server side to generate a process to listen to the appropriate port, so that you can bypass the client installed a firewall problem.
An active mode FTP connection is established to follow these steps:
1. The client opens a random port (the port number is greater than 1024, where we call it X), and an FTP process connects to the server's 21st command port. At this point, the source port is random port X, on the client side, the remote port is 21, on the server.
2. The client starts listening on the port (x+1) and sends a port command to the server (via the server's 21st command port), which tells the server that the client is listening on the port number and is ready to receive data from this port. This port is the data port we know of.
3. The server opens the source port # 20th and establishes a connection to the client data port. At this point, the source port is 20 and the remote Data port is (x+1).
5. The client establishes a connection to the server 20th port via the local data port, and then sends an answer to the server telling the server that it has established a connection.
FXP says the simple point is that an FTP client controls two FTP servers and transfers files between two FTP servers. FXP is called the File Exchange protocol―― Files Exchange protocol, you can think FXP itself is actually a subset of FTP, because the FXP method is actually the use of the FTP Server Proxy command, but its prerequisite is that the FTP server to support PASV , and supports FXP mode. FXP transfer, the file is not downloaded to local, local only send control command, so FXP transfer speed only with the network speed between two FTP servers, and local speed independent. Because the FXP method local only send commands, so after the start of the transfer, as long as the local do not send the Stop command, even if the local shutdown, FXP is still transmitting, until a file transfer is complete or file transfer error, the FTP server waits for local Send command, will not be able to receive commands and terminate the FXP delivery.
Because of the above reasons, when the FXP transmission error, the local user process is still left in the FTP server, and does not exit, so when the FTP server again, may be due to the user thread exceeded the Allow, the FTP server prompts the client has logged in and denied the client connection, The FTP server cannot be connected again until the puppet process on the server is killed by the FTP server for timeouts or other reasons.
There are two prerequisites for a successful FXP: ① two FTP servers support Fxp;② two FTP servers support PASV mode. However, it does not mean that the FTP server satisfying both conditions is successfully fxp by local operation, which is also related to the local network condition of the FTP server. So sometimes there will be the same two FTP, others can FXP, and you can not the situation.
TFTP (Trivial file Transfer Protocol) Small File Transfer Protocol
It is a Web application, it is simpler than FTP and less than FTP function. It uses the UDP protocol instead of the TCP protocol when it does not require user permissions or directory visibility.
TFTP is a simple protocol for transferring files, which is based on the UDP protocol, but we are not sure that some TFTP protocols are based on other transport protocols. This protocol is designed for small file transfers, so it does not have many of the usual FTP features, it can only be obtained from the file server or write to the file, cannot list the directory, not authentication, it transmits 8 bits of data. There are three modes in the transmission: NETASCII, this is the 8-bit ASCII format, and the other is octet, which is the 8-bit source data type; the last mail is no longer supported, it returns the returned data directly to the user instead of saving as a file.
Any transfer from a request to read or write to a file is also a connection request. If the server approves this request, the server opens the connection and the data is transmitted in a fixed length of 512 bytes. Each packet consists of one piece of data, and the next packet must be confirmed by the client prior to the last packet being issued by the server. If the size of a packet is less than 512 bytes, the transport structure is represented. If the packet is lost during transmission, the sender retransmits the last unacknowledged packet after time-out. Both sides of the communication are the issuer and receiver of the data, one party transmits the data to receive the reply, the other party sends the reply to receive the data. Most errors cause the connection to break and the error is caused by an incorrect packet. The package will not be confirmed and will not be re-sent, so the other party cannot receive it. If the error packet is missing, a time-out mechanism is used. The error is mainly caused by the following three situations: The request cannot be satisfied, the contents of the received packet is wrong, and the error cannot be explained by delay or re-send, and the access to the resource is lost (such as hard disk full). TFTP only in one case does not interrupt the connection, in which case the source port is incorrect, in which case the packet that indicates the error is sent to the source machine. This agreement is much more restrictive, and it is all done to make it easier to implement.
Common FTP commands under Linux:
1. Connect to the FTP server
Format: FTP [hostname| ip-address]
A) under the Linux command line, enter:
b) The server asks for your user name and password, enter the user name and the corresponding password, respectively, to be certified.
2. Download the file
The download file usually uses the get and mget two commands.
Format: Get [Remote-file] [Local-file]
Transfer files from the remote host to the local host.
To get/usr/your/1.htm on a remote server,
Ftp> get/usr/your/1.htm 1.htm (return)
Format: mget [Remote-files]
Receives a batch of files from the remote host to the local host.
To get all the files under/usr/your/on the server,
Ftp> mget * * (carriage return)
Each time you download a file, you will be prompted. If you want to remove the hint, do it before the mget. * * Command:prompt off
Note: The files are downloaded to the current directory of the Linux host. For example, the FTP command that runs under/usr/my, the files are downloaded to/usr/my.
3. Uploading Files
Format: Put Local-file [Remote-file]
Transfers a local file to the remote host.
If you want to transfer the local 1.htm to the remote host/usr/your, and renamed to 2.htm
Ftp> put 1.htm/usr/your/2.htm (carriage return)
Format: Mput local-files
Transfers a batch of files from the local host to the remote host.
To upload all the HTML files in the local current directory to the server/usr/your/
Ftp> Cd/usr/your (carriage return)
Ftp> mput *.htm (return)
Note: The upload file is from the current directory of the host. For example, the FTP command running under/usr/my will only be uploaded to the server/usr/your under/usr/my file Linux.
4. Disconnect the connection
Bye: Interrupts the connection to the server.
Ftp> Bye (carriage return)
Before implementing the code, you need to know:
The FTP protocol is more complex than other protocols, such as the HTTP protocol. and the general C/s application is different from the general C/s application generally will only establish a Socket connection, this connection processing both server-side and client connection commands and data transmission. In the FTP protocol, the method of transferring commands and data separately is more efficient.
FTP uses 2 ports, a data port, and a command port (also called a control port). These two ports are typically 21 (command port) and 20 (data port). Control sockets are used to transfer commands, and data sockets are used to transmit data. After each FTP command is sent, the FTP server will return a string that includes a response code and some descriptive information. The return code is primarily used to determine whether the command was executed successfully.
In general, the client has a port that the Socket uses to connect to the FTP server, which is responsible for sending and receiving the response information returned by the FTP command. Some operations, such as "Login", "Change Directory", "Delete Files", rely on this connection to send commands to complete.
For the operation of data transfer, mainly display directory list, upload, download files, we need to rely on another socket to complete.
If you use passive mode, the server side typically returns a port number. The client needs to use a different Socket to connect to the port, and then we can send the command according to the operation, and the data will be transmitted through a newly opened port.
If active mode is used, the client usually sends a port number to the server and listens on this port. The server needs to connect to the data port opened by the client and transmit the data.
FTP each command has 3 to 4 letters, the command followed by parameters, separated by a space. Each command ends with "\ r \ n".
To download or upload a file, first log in to the FTP server, then send a command, and finally exit. In this process, the main commands used are USER, PASS, SIZE, REST, CWD, RETR, PASV, PORT, QUIT.
User: Specifies the username. This is usually the first command issued after controlling the connection. "User gaoleyi\r\n": The username is gaoleyi login.
PASS: Specify the user password. The command immediately follows the USER command. "PASS gaoleyi\r\n": Password is gaoleyi.
Size: Returns the sizes of the specified files from the server. "Size file.txt\r\n": Returns the size of the file if the File.txt file exists.
CWD: Changing the working directory. such as: "CWD dirname\r\n".
PASV: Let the server listen to the data port and enter the passive mode. such as: "pasv\r\n".
Port: Tell the FTP server client to listen on the port number and have the FTP server connect to the client in active mode. such as: "PORT h1,h2,h3,h4,p1,p2".
RETR: Download the file. "RETR file.txt \ r \ n": Download file file.txt.
STOR: Uploading files. "STOR file.txt\r\n": Upload file file.txt.
REST: The command does not transfer the file, but rather skips the data after the specified point. This command should be followed by other FTP commands that require file transfer. "REST 100\r\n": Re-specify a file transfer offset of 100 bytes.
QUIT: Closes the connection to the server.
After the client sends the FTP command, the server returns a response code.
The response code is represented by a three-bit digital code:
The first number gives a general indication of the state of the command, such as a successful, failed, or incomplete response.
The second number is the classification of the response type, such as 2 for connection-related responses, and 3 for user authentication.
The third number provides more detailed information.
The first number has the following meanings:
1 indicates that the server received the information correctly and has not yet been processed.
2 indicates that the server has handled the information correctly.
3 indicates that the server received the information correctly and is processing it.
4 indicates a temporary error in the information.
5 indicates an information permanent error.
The second number has the following meanings:
0 means syntax.
1 indicates system status and information.
2 indicates the connection status.
3 represents information related to user authentication.
4 means undefined.
5 represents information about the file system.
The main steps for Socket client programming are as follows:
Socket () to create a socket
Connect () to the server
Write () and read () session
Close () closes the Socket
The main steps for Socket server-side programming are as follows:
Socket () to create a socket
Listen () Monitor
Accept () requests to receive connections
Write () and read () session
Close () closes the Socket
Let's take an example to have an in-depth understanding of the FTP client. The FTP client implemented in this article has the following features:
A Socket connection is established between the client and the FTP server.
Send the USER, PASS command to the server to log on to the FTP server.
Use the PASV command to get the port number that the server listens on to establish a data connection.
Use the retr/stor command to download/upload files.
Disconnect the data connection and send the Quit command after the download is complete.
During the entire interaction, the control connection is always in a connected state, and the data connection is opened and closed when each file is transmitted.
When the client establishes a connection to the server, the server returns a 220 response code and some welcome information.
When the client sends a user name and password, the server verifies that a 230 response code is returned. The client can then send commands to the server side.
When the client is downloading/uploading the file, send the command to get the server into passive mode. The server opens the data port and listens. and returns the port number of the response code 227 and the data connection.
When the client sends a command to download the file. The server returns a response code of 150 and sends the file contents to the data connection.
When the client has finished downloading, send a command to exit the server and close the connection. The server returns a response code of 200.
It is important to note that when you send the FTP command, you should immediately follow the "\ r \ n" after the command, or the server will not return information. Carriage return the newline symbol "\ r \ n" is the end of the FTP command, and when the server receives this symbol, it is assumed that the command sent by the client has ended and started processing. Otherwise it will continue to wait.
When the client sends a command to upload a file, the server receives the file from the data connection.
So far, this article describes the client in passive mode for file upload and download. The following describes the client download file in active mode.
This article is from the "Small Stop" blog, please be sure to keep this source http://10541556.blog.51cto.com/10531556/1818256
FTP-------Application Layer protocol
Start building with 50+ products and up to 12 months usage for Elastic Compute Service