-
10.2.4.3 Example 3: Development of network application layer protocols
Tsinghua University Press "Java Programmers, Things to Do at Work" author: Zhong Sheng - Partial excerpts from Chapter 10 "A master is as talented as he is capable".
Everyone may have used FTP upload and download tools. For example, "LeapFTP" is a very convenient FTP server upload and download tool, as shown in the figure. This tool is very convenient. After entering the username and password, you can see the file list on the FTP server, which facilitates upload and download operations.
Have you ever tried to write an FTP file upload and download application in Java?
Java can also develop such a program. It is not complicated. Let's first look at how to develop an FTP application using Java's FTP toolkit. Please see the following program:
import sun.net.*;
import sun.net.ftp.*;
public class FTP {
public static void main(String[] args) {
String server="192.168.0.12"; //Input the IP address of the FTP server
String user="useway"; //Username to log in to the FTP server
String password=" !@#$%abce "; //Password of the username to log in to the FTP server
String path="/home/useway"; //The path on the FTP server
try {
FtpClient ftpClient=new FtpClient(); //Create FtpClient object
ftpClient.openServer(server); //Connect to FTP server
ftpClient.login(user, password); //Log in to the FTP server
if (path.length()!=0) ftpClient.cd(path);
TelnetInputStream is=ftpClient.list();
int c;
while ((c=is.read())!=-1) {
System.out.print((char)c);
}
is.close();
ftpClient.closeServer();//Exit the FTP server
}
catch(Exception ex){
}
}
}
If you are interested, you can write this program yourself. When this program is run, we will see the situation as shown in the figure, listing the directory contents of the server-side program.
This program is a simple program to get the file list on the FTP server, but don't get me wrong, this program cannot be called the development of a "Network Application Layer Protocol" program!
This program only uses the related classes in "sun.net.*;" and "sun.net.ftp.*;" to operate the FTP side. We do not use Java's Socket at the network level to face the FTP server. Any request is sent to the client, but a link request is sent to the server through the toolkit provided by Java.
The advantage of using Java's FTP package to connect to the FTP server is that we don't need to care about the specific details of sending data at the network level, but only need to call the corresponding method. The disadvantage of using Java's FTP package to connect to the FTP server is that developers do not know the ins and outs of application layer protocol sending and receiving, cannot understand the principles, and have a very weak grasp of the underlying data.
At this point, some programmers will ask: "So how does FTP interact between the PC and the server at the network level?" Well, let me list the FTP protocol interaction process for you.
Please look at the following example of FTP protocol interaction:
FTP servers: 220 (vsFTPd 2.0.1)
FTP client: USER useway
FTP server: 331 Please specify the password.
FTP client: PASS !@#$%abce
FTP server: 230 Login successful.
FTP client: CWD /home/useway
FTP server: 250 Directory successfully changed.
FTP client: EPSV ALL
FTP server: 200 EPSV ALL ok.
FTP client: EPSV
FTP server: 229 Entering Extended Passive Mode (|||62501|)
FTP client: LIST
FTP server: 150 Here comes the directory listing.
FTP server: 226 Directory send OK.
FTP client: QUIT
FTP server: 221 Goodbye.
The above text is actually the process of interaction between the FTP server and the FTP client. The protocol for transmitting information between them is the TCP protocol, and the content sent to each other is what is written in the above text.
Let’s explain the meaning of each sentence step by step:
FTP server: 220 (vsFTPd 2.0.1) | Description: Link successful
FTP client: USER useway |Instructions: Enter username
FTP server: 331 Please specify the password. |Description: Please enter the password.
FTP client: PASS !@#$%abce |Instructions: Enter password
FTP server: 230 Login successful. | Description: Login successful.
FTP client: CWD /home/useway | Description: Switch directory
FTP server: 250 Directory successfully changed. |Description: Directory switch was successful.
FTP client: EPSV ALL | Description: EPSV passive link mode
FTP server: 200 EPSV ALL ok. | Description: OK
FTP Client: EPSV | Description: Link
FTP server: 229 Entering Extended Passive Mode (|||62501|) |Description: The passive link port is 62501
FTP client: LIST | Description: Execute LIST to display the file list
FTP server: 150 Here comes the directory listing. | Description: The list is sent from port 62501
FTP server: 226 Directory send OK. |Description: Send completed
FTP client: QUIT | Description: Quit FTP
FTP server: 221 Goodbye. | Description: Goodbye
With the content of the above text, we can get the FTP file list without any tools. If you don’t believe it, follow me and do it again.
Step 1: First open CMD to enter the DOS command line mode and type:
telnet 192.168.0.1 21[Enter]
Description: Telnet to port 21 of the Ftp server.
After executing this command, the results obtained are as shown in the figure.
Have you found any problems?
The content of the prompt is exactly the first sentence of the above paragraph: 220 (vsFTPd 2.0.1), which means that the FTP server has accepted our link and can proceed to the next step.
Step 2: Type the following series of sending contents one by one:
USER useway[Enter]
PASS !@#$%abce [Enter]
CWD /home/useway[Enter]
EPSV ALL[Enter]
EPSV[Enter]
The results obtained are shown in the figure.
Well, this time the FTP server gave a series of responses, and finally gave a new port number "58143".
Step 3: Open a new CMD window and type:
telnet 192.168.0.1 58143[Enter]
Note that the port number of the Telnet request to connect to the server this time is "58143", which is a link port given to us by the FTP server. After linking, the window is blank without any prompts, as shown in the figure.
Step 4: Return to the first CMD window and type:
LIST[Enter]
Step 5: At this time, the second CMD window receives the file list:
The second window receives the file list as shown in the figure.
Step 6: Exit the operation
QUIT[Enter]
After the execution is completed, the link to the host is lost, as shown in the figure.
As you can see, the FTP protocol is such an interactive process. You can also complete these basic command operations of FTP by using the Telnet tool that comes with the system. If you want to use Java's Socket to complete the above operation, you only need to follow the above content step by step to send the string to the FTP server.
We also give example code below:
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
public class FTPClient{
public static void main(String[] args) throws Exception{
Socket socket = new Socket("192.168.0.1",21);
InputStream is = socket.getInputStream();
OutputStream os = socket.getOutputStream();
//Receive initial link information
byte[] buffer = new byte[100];
int length = is.read(buffer);
String s = new String(buffer, 0, length);
System.out.println(s);
//Send username
String str = "USER usewayn";
os.write(str.getBytes());
//Get the return value
length = is.read(buffer);
s = new String(buffer, 0, length);
System.out.println(s);
//Send password
str = "PASS !@#$%abcdn ";
os.write(str.getBytes());
//Get the return value
length = is.read(buffer);
s = new String(buffer, 0, length);
System.out.println(s);
//Send switching folder command
str = "CWD /home/usewayn";
os.write(str.getBytes());
//Get the return value
length = is.read(buffer);
s = new String(buffer, 0, length);
System.out.println(s);
//Set mode
str = "EPSV ALLn";
os.write(str.getBytes());
//Get the return value
length = is.read(buffer);
s = new String(buffer, 0, length);
System.out.println(s);
//Get passive monitoring information
str = "EPSVn";
os.write(str.getBytes());
//Get the return value
length = is.read(buffer);
s = new String(buffer, 0, length);
System.out.println(s);
//Get the port number for FTP passive monitoring
String portlist=s.substring(s.indexOf("(|||")+4,s.indexOf("|)"));
System.out.println(portlist);
//Instantiate the ShowList thread class and link the FTP passive listening port number
ShowList sl=new ShowList();
sl.port=Integer.parseInt(portlist);
sl.start();
//Execute LIST command
str = "LISTn";
os.write(str.getBytes());
//Get the return value
length = is.read(buffer);
s = new String(buffer, 0, length);
System.out.println(s);
//Close the link
is.close();
os.close();
socket.close();
}
}
//Get the passive link information class, this class is multi-threaded
class ShowList extends Thread{
public int port=0;
public void run(){
try{
Socket socket = new Socket("192.168.0.1",this.port);
InputStream is = socket.getInputStream();
OutputStream os = socket.getOutputStream();
byte[] buffer = new byte[10000];
int length = is.read(buffer);
String s = new String(buffer, 0, length);
System.out.println(s);
//Close the link
is.close();
os.close();
socket.close();
}
catch(Exception ex){
}
}
}
The running result obtained after running the program is as shown in the figure. It is basically the same as the above running effect. What about the bottom layer? It is nothing more than unpacking the encapsulated methods and running them. As long as we understand the rules of their operation, we can The same program can be developed.