FTP command
The main FTP operations are based on various commands. Common Commands include:
◆ Set transmission mode, which includes ASC ⅱ (text) and binary mode;
◆ Directory operations: change or display the current directory of the remote computer (Cd, DIR/LS command );
◆ Connection operation. The open command is used to establish a connection with a remote computer. The close command is used to close the connection;
◆ Sending operation. The PUT command is used to send files to a remote computer. The mput command is used to send multiple files to a remote computer;
◆ Get operation. The GET command is used to receive a file. The mget command is used to receive multiple files.
Programming Logic
According to the working principle of FTP, create a server socket port in the main function and wait for the client request. Once the client request is accepted, the server program will establish a server thread separation to process client commands. If the client needs to transfer files with the server, a new socket connection is established to complete file operations.
Programming Skills
1. Main Function Design
In the main function, listener the server port and create a service thread. We use a static string variable initdir to save the working directory of the server line runtime. The initial working directory of the server is input by the user when the program is running. The default is the root directory of drive C.
The specific code is as follows:
Public class ftpserver extends thread {
Private socket socketclient;
Private int counter;
Private Static string initdir;
Public static void main (string [] ARGs ){
If (ARGs. length! = 0 ){
Initdir = ARGs [0];
} Else {initdir = "C :";}
Int I = 1;
Try {
System. Out. println ("FTP server started! ");
// Listen to port 21
Serversocket S = new serversocket (21 );
For (;;){
// Accept client requests
Socket incoming = S. Accept ();
// Create a service thread
New ftpserver (incoming, I). Start ();
I ++;
}
} Catch (exception e ){}
}
2. Thread class design
The main design of the thread class is implemented in the run () method. Use the run () method to obtain the socket information of the client, obtain the input stream and output stream based on the socket, and send the welcome information to the client.
3. FTP command processing
(1) access control commands
◆ The code for processing the user name (user) and password (PASS) commands is as follows:
If (Str. startswith ("user ")){
User = Str. substring (4 );
User = user. Trim ();
Out. println ("331 password ");}
If (Str. startswith ("pass "))
Out. println ("230 user" + User + "logged in .");
The USER command and the PASSWORD command are used to submit the user name and password entered by the client user respectively.
◆ The CWD (change working directory) command processing code is as follows:
If (Str. startswith ("CWD ")){
String str1 = Str. substring (3 );
Dir = dir + "/" + str1.trim ();
Out. println ("250 CWD command succesful ");
}
This command changes the working directory to the user-specified directory.
◆ The cdup (change to parent directory) command processing code is as follows:
If (Str. startswith ("cdup ")){
Int n = dir. lastindexof ("/");
Dir = dir. substring (0, N );
Out. println ("250 CWD command succesful ");
}
This command changes the current directory to the previous directory.
◆ The quit command processing code is as follows:
If (Str. startswith ("quit ")){
Out. println ("good bye ");
Done = true;
}
This command exits and closes the connection with the server, and outputs good bye.
(2) transmission parameter command
◆ The PORT command processing code is as follows:
If (Str. startswith ("Port ")){
Out. println ("200 PORT command successful ");
Int I = Str. Length ()-1;
Int J = Str. lastindexof (",");
Int K = Str. lastindexof (",", J-1 );
String str1, str2;
Str1 = "";
Str2 = "";
For (int l = k + 1; lstr1 = str2 + Str. charat (L );
}
For (int l = J + 1; L <= I; l ++ ){
Str2 = str2 + Str. charat (L );
}
Tempport = integer. parseint (str1) * 16*16 + integer. parseint (str2 );
}
When using this command, the client must send the 32-bit IP address used by the client to receive data and the 16-bit TCP port number. The information is transmitted in decimal format and separated by commas.
◆ The type command processing code is as follows:
If (Str. startswith ("type ")){
Out. println ("200 type set ");
}
The type command is used to complete the type setting.
(3) FTP service commands
◆ Code processed by RETR (reteieve) and store (store) commands
If (Str. startswith ("retr ")){
Out. printlns ("150 binary data connection ");
STR = Str. substring (4 );
STR = Str. Trim ();
Randomaccessfile OUTFILE = new
Randomaccessfile (DIR + "/" + STR, "R ");
Socket tempsocket = new socket (host, tempport );
Outputstream outsocket
= Tempsocket. getoutputstream ();
Byte bytebuffer [] = new byte [1024];
Int amount;
Try {
While (amount = OUTFILE. Read (bytebuffer ))! =-1 ){
Outsocket. Write (bytebuffer, 0, amount );
}
Outsocket. Close ();
Out. println ("226 transfer complete ");
OUTFILE. Close ();
Tempsocket. Close ();
}
Catch (ioexception e ){}
}
If (Str. startswith ("stor ")){
Out. printlns ("150 binary data connection ");
STR = Str. substring (4 );
STR = Str. Trim ();
Randomaccessfile infile = new
Randomaccessfile (DIR + "/" + STR, "RW ");
Socket tempsocket = new socket (host, tempport );
Inputstream insocket
= Tempsocket. getinputstream ();
Byte bytebuffer [] = new byte [1024];
Int amount;
Try {
While (amount = insocket. Read (bytebuffer ))! =-1 ){
Infile. Write (bytebuffer, 0, amount );
}
Insocket. Close ();
Out. println ("226 transfer complete ");
Infile. Close ();
Tempsocket. Close ();
}
Catch (ioexception e ){}
}
File Transfer Commands include obtaining file RETR from the server and Sending File stor to the server. These two commands are very similar in processing. When processing the RETR command, first obtain the name of the file to be obtained by the user, create a file input stream based on the name, then establish a temporary socket connection with the client, and get an output stream. Then, read the data in the file input stream and send it to the client using the socket output stream. After the transmission is complete, close the stream and temporary socket.
The processing of the STOR command is the same, but the opposite is true.
◆ The DELE (delete) command processing code is as follows:
If (Str. startswith ("DELE ")){
STR = Str. substring (4 );
STR = Str. Trim ();
File file = new file (Dir, STR );
Boolean del = file. Delete ();
Out. println ("250 DELETE command successful ");
}
The DELE command is used to delete specified files on the server.
◆ The list command processing code is as follows:
If (Str. startswith ("list ")){
Try {
Out. println ("150 ASCII data ");
Socket tempsocket = new socket (host, tempport );
Printwriter out2 = new printwriter (tempsocket. getoutputstream (), true );
File file = new file (DIR );
String [] dirstructure = new string [10];
Dirstructure = file. List ();
String strtype = "";
For (INT I = 0; iIf (dirstructure [I]. indexof (".") =-1 ){
Strtype = "D ";}
Else
{Strtype = "-";}
Out2.println (strtype + dirstructure [I]);
}
Tempsocket. Close ();
Out. println ("226 transfer complete ");
}
Catch (ioexception e ){}
The list command returns the directory structure under the working directory of the server to the client, including the list of files and directories. When processing this command, create a temporary socket to send the directory information to the client. The default port number of this socket is 1, and then a file object is created for the current working directory () method to obtain a string array containing the names of all files and sub-directories in the directory, and then determine whether the name contains the Special ". to distinguish between directories and files. Finally, the obtained name array is sent to the client through a temporary socket.