Ftp client instructions in Linux

Source: Internet
Author: User
Tags ftp protocol
For more information about the ftp client in Linux, see Linux Enterprise Application-Linux server application. L software features:

Based on the socket function in linux, this program uses the ftp protocol to connect to the ftp server in linux, upload and download files and the entire folder, and browse the content of the ftp server.

L instructions for use:

// Usage:

// Connect: ftp. out 10.10.10.10 21

// Download file: get filename

// Upload file: put filename

// Download directory: d filename

// Upload directory: u filename

// List: list

// List: dir

// Quit: quit



L function subject description:

Enum COMMAND getcmd (struct ftp_client *); // get user input

Int getline (char *); // get a line of input

Int getport (char *); // get port of data socket

Int pasv (struct ftp_client *); // enter pasive mode in ftp

Int list (struct ftp_client *); // list file in server

Int get (struct ftp_client *); // download file

Int cwd (struct ftp_client *); // change directory of ftp server

Int get_d (struct ftp_client *); // download dirctory

Int print_list (struct ftp_client *); // print file name in a list file

Int get_file (struct ftp_client *); // downlaod file

Int analyze (struct ftp_client *); // got name of the file and directory





L ftp protocol description:

File Transfer Protocol (FTP)

1. Introduction

The goal of FTP is to improve file sharing and provide non-direct use of remote computers, so that the storage media can transmit data transparently and reliably and efficiently to users.



2. Data Transmission

Data connections only transmit data and control connection transmission commands and responses.



3. Transmission Mode



There are three transmission modes: one is to format the data and consider restarting the process; the other is to compress the data; the other is to transfer the data without processing (a small amount of processing.



4. Stream Mode

Data is transmitted in byte streams. There is no limit on the type of representation used, and the record structure is allowed.



5. Access Control commands

The following command specifies the access control flag (the authorization code is in brackets ):

USER)

The parameter indicates the user's Telnet string.

Password (PASS)

The parameter is a Telnet string that marks the user password. This command follows the USER command, which is an indispensable step for access control on some sites.

Change working directory (CWD)

This command allows users to work in different directories or datasets without changing their logon or account information. The input parameters remain unchanged. A parameter is generally a directory name or a collection of system-related files.



6. transmission parameter command

All data transmission parameters have default values. The server must record the default value, which can be sent in any order after FTP service requests. The following command transfers parameters:

Data PORT)

The parameter is the data connection port to be used. Generally, no command response is required. If you use this command, you need to send a 32-bit IP address and a 16-bit TCP port number. The above information is transmitted in decimal format with a comma (,), as shown in the following example:

PORT h1, h2, h3, h4, p1, p2

H1 is the maximum 8-bit IP address.



7. Passive (PASV)

This command requires the server DTP to listen on the specified data port and enter the passive request receiving status. The parameters are the master machine and port address.



8. TYPE)

The parameter specifies the type.



9. FTP service commands

Get file (RETR)

This command allows the server DTP to send a copy of the file in the specified path to the server or user DTP. The status and content of the files on the server are not affected.

Save (STOR)

This command allows the server DTP to receive the data transmitted over the data connection and save the data in the file of the server. If the file already exists, the original file will be overwritten. If the file does not exist, create a new file.

Create directory (MKD)

This command creates a new directory in the specified path.

Print the working directory (PWD)

In the response, the current working directory is returned.

System (SYST)

Used to determine the operating system running on the server.

10. FTP Response

1yz confirm the prepared response

The requested operation is being initialized; wait for another response before entering the next command. This type of response is used to describe whether the command is accepted and how to monitor the synchronization is difficult during implementation. The user process can now ignore the data connection. The FTP process on the server returns a 1yz response to almost all the first commands.

2yz confirm to complete the response

The required operation has been completed. You can execute a new command.

3yz determines the intermediate response

The command is accepted, but the required operation is stopped. Stop receiving updated information.

4yz temporarily refused to complete the response

If the command is not accepted, the required operation is not executed, but the error is temporary. You can send the message again later. The user should return the start of the command sequence. This command is temporarily uncertain. The purpose of this command is to let the user process try to use this command again.

5yz always refuses to complete the response

The difference between it and the temporary refusal to complete the response is that the error condition is that half past one will not disappear.

X0z format error;

X1z this type of response is for request information;

X2z is related to control and data connection;

X3z about the authentication and account logon process;

X4z not used;

X5z is related to the file system;



The following lists the Response codes in numerical order and their meanings:

125

Data Connection enabled, ready for transfer

150

The file is in good state and data connection is enabled.

200

Command successful

202

Command not implemented

215

Name System Type

220

Prepare services for new users

221

The service closes the control connection and can log out.

225

Data Connection is enabled, no transmission is in progress

226

The data connection is closed and the requested file operation is successful.

227

Enter passive mode

230

User Logon

250

Requested file operation completed

331

The user name is correct. A password is required.

332

Account Information Required for Logon

421

Cannot provide services. Close the control connection

425

Cannot open data connection

426

Disconnect and stop transmission

450

Requested file operation not executed

451

Abort request operation: there is a local error

500

Format error. The command is not recognizable

501

Parameter syntax error

502

Command not implemented

503

Command Order Error

530

Not logged on

550

Requested operation not performed

551

Request operation aborted: Unknown page type

552

The requested file operation is aborted, and the storage allocation overflows.

553

Action not requested: invalid file name



L analysis of technical difficulties:

1. After the list Command, the list_ftp file name is processed. The following is the implemented algorithm:

This part of content is implemented by the analyze function:

Int analyze (struct ftp_client * p)

{







Open a file

While (the file is not finished)

{

Get a character

Mark whether it is a file

Skipping 8 strings

Save the remaining full reads as names

If it is a file

Push to stack

Create this file

}

}



2. Stack operations when downloading directories.



Int get_local_file (struct ftp_client * p) // upload all the file in this level and push directory to stack

{

Back up p-> path first

Strcpy (recover, p-> path );

While (p-> path [n ++]); // n is current path length

Calculate the path length.

Analyse_local (p); // name is store in p-> buf

Analyze the file structure of the current layer, download the file, and press the stack folder

While (p-> tell> 0) when there are folders in the stack

{

If it is a file

If (p-> file [p-> tell-1] = 0) // = file why don't tell the file name? File name

{

Download files

}

For folders

Else if (p-> file [p-> tell-1] = 1) // = directory

{

Push folders to Stacks one by one

}

P-> tell --; // one file in current directory is processed

}

Return 1;

}







L further improvement

1. the error handling mechanism should be improved, and errors often occur on the network. Therefore, it cannot be assumed that all transmission is as expected. At present, this program reports errors, but after an error occurs, in some cases, the program is aborted after an error is reported, instead of returning to the main loop. You need to log on again, which is troublesome. Protocol ports can be transmitted without blocking, and a constant try_time can be used to control the number of detection times. This makes it easier to use this program.

2. You can consider implementing the function that can be changed in the current local directory.

3. You can consider implementing more ftp functions, such as deleting files on the server.

4. Constant buffer is used in the stack operation, which is a waste of memory. You can change it to malloc to dynamically allocate the name length and buffer length. In this case, writing is prone to errors.

5. You can consider porting it to windows. You only need to replace the socket part with winsock. There is also a wsastartup function.

6. You can consider porting to the graphic interface and programming with qt or gtk.



L experiences:

1. the error focus and troubleshooting focus are still boundary processing. gdb or kdevelop can be used for debugging, but without the convenience of vc, you can copy the main algorithm to vc to eliminate the error, the function of automatically adding variables and shortcut keys can improve efficiency.

2. The data structure is still the key to algorithms, and stack processing is also an important part of the data structure. We should try our best to be proficient in application. The traversal of Binary trees should be reviewed.

3. programming in Linux is different in windows. There is no such large framework as mfc, but there are also many small commands that can be used as much as possible to use functions and functions in shell, such as ls, there are rm, mkdir, and so on. You can use system call or exec function.

4. debugging of socket network programming is troublesome and there is no good way. My experience is to use a large number of printf statements to output important variables, then, you can trace and analyze the problems on the screen. Debugging with kdevelop is good, that is, variables cannot be automatically added, and you have to re-enter each call.

5. modularization or objectization should be considered for programming to facilitate further transplantation.
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.