With the increasing popularity of Java language, especially the close combination of Java and Internet Web, it has achieved great success in the world. Java has become a good development platform and operating environment in the construction of modern information systems with its unique features such as platform, object-oriented, distributed, multi-clue, and perfect security mechanism.
I. Java Network Application Model
Like many environments on the Internet, the complete Java application environment is actually a client/Server environment, more specifically the Browser/Server model (that is, the Browser/Server model, (Web model ). However, unlike the two-layer structure of traditional Client/Server (C/S), the Web model of Java is composed of three layers. The traditional C/S structure uses the message transmission mechanism. The client sends a request to the server, and the server processes the request and sends it back to the client through the transmission mechanism. In the Web model, the server end is divided into two parts: Application Server (Web Server) and database server.
Java provides excellent support for distributed computing environments through its network library. Java provides a URL (Uniform Resource Locator) object for data distribution. This object can be used to open and access objects on the network. The access method is almost the same as accessing the local file system. For Operation distribution, the Java Client/Server mode can distribute operations from the server to the client end (the server is responsible for providing query results, and the client is responsible for organizing the display of results ), in this way, the execution efficiency of the entire system is improved and dynamic scalability is increased. Java Network library is an extension of the Java language to adapt to the Internet environment. In addition, to adapt to the continuous development of the Internet, Java also provides dynamic expansion protocols to continuously expand Java Network Libraries.
The Java Network Library supports multiple Internet protocols, including Telnet, FTP, and HTTP (WWW). The corresponding subclass library of the Java Network Library is:
Java.net
Java.net. ftp
Java.net. www. content
Java.net.www.html
Java.net. www. http
These sub-libraries each contain classes and methods that can be used to process Internet protocols. Java.net is used to process some basic network functions, including remote Logon (Telnet) and java.net. ftp is used to process the ftp protocol; java.net. www. content is used to process the content of WWW pages, including java.net.www.html and java.net. www. http provides support for the HTML language and HTTP protocol respectively.
2. Java applications in the Client/Server Environment
The client/server uses the connection-based network communication mode during distributed processing. This communication mode first defines a communication protocol between the client and the server, creates a Socket class, and uses this class to establish a reliable link. Then, the Client/Server reliably transmits data on this link. The client sends a request, the server listens to the request from the client, and provides the response service for the client. This is a typical "request-response" mode. The following is a typical operation process of the Client/Server:
1. The server listens to the corresponding port input;
2. The client sends a request;
3. The server receives the request;
4. The server processes the request and returns the result to the client;
5. Repeat the above process until a session is completed.
According to the above process, we use Java to compile an Application for the server and client respectively ). When the program is on the server, the program is responsible for listening to client requests and establishing Socket connections for each client request to provide services for the client. The service provided by this program is: Read a line of text from the client, reverse the text, and send it back to the client.
Through this program example, we can see that when designing a C/S program using Java, you need to pay attention to the following points:
(1) The server should use the ServerSocket class to process client connection requests. When the client connects to the port listened by the server, ServerSocket allocates a new Socket object. The new Socket object will be connected to some new ports to process communications with the corresponding client. Then, the server continues to listen to ServerSocket to process new client connections.
Socket and ServerSocket are two classes provided by the Java Network Library.
(2) The server uses a multithreading mechanism. The Server object itself is a thread, and its run () method is an infinite loop to listen to connections from clients. Each time a new client is connected, ServerSocket creates a new Socket class instance, and the server also creates a new thread, that is, a Connection object, to process Socket-based communication. All communication with the client is handled by the Connection object. The constructor of Connection initializes the communication stream based on the Socket object and starts the thread running. Communication with the client and service provision are handled by the Connection object.
(3) The client first creates a Socket object to communicate with the server. Two objects are created: DataInputStream and PrintStream. The former is used to read data from the InputStream input stream of the Socket, and the latter is used to write data to the OutputStream of the Socket. Finally, the client program reads data from the standard input (such as the console), writes the data to the server, reads the Response Message from the server, and writes the Response Message to the quasi-output.
The following is a list of source programs on the server and client respectively. In the NT 4.0 network environment (TCP/IP), JDK is used for debugging.
3. Write server-class Java programs
// Server. java
Import java. io .*;
Import java.net .*;
Public class Server extends Thread
{
Pubfinal static int Default_Port = 6543;
Protectd int port;
Protectd ServerSockt listen_socket;
// Define an error routine: exit the program if an exception occurs.
Public static void fail (Exception e, String msg)
{
System. err. println (msg + ":" + e );
System. exit (1 );
}
// Define and start the Socket routine of the server to listen to client connection requests.
Public Server (int port)
{
If (port = 0) port = Default_Port;
This. port = port;
Try
{
Listen_socket = new ServerSocket (port );
}
Catch (IOException e) fail (e, "Exception creating server socket ");
System. out. println ("Server: listening on port" + port );
This. start ();
}
/* The following is the main program of the server listening thread. This thread executes cyclically all the time, listens to and accepts the connection from the client.
Request. For each connection, a connection object is generated and corresponds to it, which is communicated through the Socket channel. */
Public void run ()
{
Try
{
While (true)
{
Socket client_socket = listen_socket.accept ();
Connection c = new Connection (client_socket );
}
}
Catch (IOException e) fail (e, "Exception while listening for connections ")
}
// Start the main program of the server
Public static void main (String args [])
{
Int port = 0;
If (args. length = 1)
{
Try port = Integer. parseInt (args [0]);
Catch (NumberFormatException e) port = 0;
}
New Server (port );
// End of the main
} // End of Server class
// The following defines the Connection class, which is used to process all the communication threads with the client.
Class Connection extends Thread
{
Protected Socket client;
Protected DataInputStream in;
Protected PrintStream out;
// Initialize the communication stream and start the thread
Public Connection (Socket client_socket)
{
Client = client_socket;
Try
{
In = new DataInputStream (client. getinputStream ());
Out = new PrintStream (client. getOutputStream ());
}
Catch (IOException e)
{
Try client. close ();
Catch (IOException e2 );
System. err. println ("Exception while getting socket streram:" + e );
Return;
}
This. start;
} // End of Connection method
// Service routine: Read a line of text, reverse text, and return text.
Public void run ()
{
String line;
StringBuffer revline;
Int len;
Try
{
For (;;)
{
// Read a line
Line = in. readline ();
If (line = null) break;
// Reverse the line
Len = line. length ();
Revline = new StringBuffer (len );
For (int I = len-1; I> = 0; I --)
Revline. insert (len-1-I; line. charAt (I ));
// Write out the reverse line
Out. println (revline );
}
Catch (IOException e );
Finally try client. close ();
Catch (IOException e2 );
}
// End of run method
}
// End of Connection class
3. Write client-class Java programs
// Client. java
Import java. io .*;
Import java.net .*;
Public class Client extends
{
Public static final int Default_Port = 6543;
// Define the error routine
Public static final void usage ()
{
System. out. println ("Usage: Java Client []");
System. exit (0 );
}
Public static void main (String args [])
{
Int port = Default_Port;
Socket s = null;
// Parse port Parameters
If (args. length! = 1) & (args. length! = 2) usage ();
If (args. length = 1)
Port = Defau