Application of Java in the ClientServer network

zhaozj2021-02-11  192

Application of Java in the Client / Server network

Sunshine Port Bureau - Liu Yang

---- With the daily benefit of Java language, it is especially the secret combination of Java and Internet Web, making it great success in the whole ball. The Java language has been independent of the platform, facing the parallelism, the distribution, multi-line cables, and has a good development of the newly-established system, which is a good open platform and transportation environment.

First, Java network application model

---- And many environments on the Internet, the complete Java should be actually a guest / server environment, and more independently is the browser / server model (ie, Browser / Server model, briefly called Web model). However, the two-layer clamping of the passenger / server (C / S) is different, and the Web model of the Java should be used in three layers. The C / S knot structure is made through the message transfer mechanism, issued by the client, sent a service server, and the server should be sent back to the customer after handling. In the Web model, the server is divided into two parties: one part is the application server (Web server), and the other part is the data library server.

Java network application model

---- Needle of the sub-format, Java, provides good support for its network libraries. For data distribution, Java provides a URL (Uniform Resource Locator) object, using this pair to open and access the objects on the network, and its access method and access local file system is completely equal. For the operation of the Java, Java's client / server model can be scattered from the server from the server to the customer (the server is responsible for the results of the check results, the client is responsible for the display of the woven results), which will increase the efficiency of the entire system, increase the dynamic exposure. The Java network class library is an expansion of Java language to adapt to the internet environment. In addition, in order to adapt the Internet, Java has also provided dynamic expansion agreements to unlock the Java network class library.

---- Java's network class library supports a variety of Internet protagonists, including Telnet, FTP, and HTTP (WWW), and the sub-library of Java network libraries that should be:

Java.net

Java.net.ftp

Java.Net.www.content

Java.Net.www.html

Java.Net.www.http

---- These sub-libraries have their own self-contained classes and methods that can be used to process the Internet protocol. Among them, java.net is used to handle some basic network functions, including remote login; java.net.ftp is used to process FTP protocols; java.net.www.content is used to process WWW page content; java.net. Www.html and java.Net.www.http provide support for HTML language and HTTP protocols, respectively. Second, the Java applications under the guests of the passenger / server

---- The customer / server is in the process of distribution, and the connection-based network communication mode is used. The communication model first defines a set of communication protocols between the client and the server, and creates a Socket class, using this class to build a reliable link;, then, the client / server can then transmit data on this chain. The customer has issued a request and serve the servant's request for the customer, and the customer will respond to service. This is a typical "please - answer" mode. The next is a customary operation of the customer? / Server:

---- 2 . Server monitoring of the input of the corresponding port;

---- 3 * Hassenger machine issued a request;

---- 5. Servers receive this request;

---- 6. Servers Treat this request and return the fruit to the customer;

---- 7. Replay the above process until it is completed into a session process.

---- Press the process, we use Java language to write a sequence of application (Application) that is part of the server and customer machine. When the program is on the servers, the program is responsible for listening to the customer request, and for each customer, please ask the Socket connection, which is available for the customer. The service provided by this program is: read from a line of text from the guest machine, reverse the text, and send it back to the customer.

---- In practice, I see that when I use the Java language to design the C / S program, I need to pay attention to the next few points:

---- (1), servers should use the Serversocket class to process the client's connection request. When the client is connected to the port of the server, ServerSocket will assign a new Socket object. This new Socket pair will connect to a new port, responsible for the communication with the guests of the guest. Then, the server continued to listen to Serversocket, and the new customer is connected.

---- Socket and Serversocket are two classes provided by the Java network class library.

---- (2), the servers used a multi-threaded machine. The Server pair is a thread, and its Run () method is an endless cycle, which is used to listen to the client's connection. Whenever there is a new client connected, Serversocket creates a new Socket class instance, and the server will also create a new thread, which is a Connection object to place the Socket-based communication. All communication with the client machine is processed by this Connection object. The Connection constructor will initially use the stream of stream based on the socket object, and start the thread. The communication with the client's machine and the delivery of services are processed by the Connection object. ---- (3), the customer first creates a socket paternity to communicate with the server. The following needs to create two pairs: DataInputStream and PrintStream, the former is used to read the data from the INPUTSTREAM input stream from the Socket, which is used to write data in the OutputStream to the Socket. At the end, the client program reads the data from the standard input (such as: control station), and writes these data to the server, and reads the answer message from the server, and then these answers are written to the quasi-output.

---- The following is a single source for servor and customer ends. The program is used to try with JDK1.1 under NT 4.0 network environment (TCP / IP).

---- 2 . Write server class Java program

// Server.java

Import java.io. *;

Import java.net. *;

Public Class Server EXTENDS THREAD

{

Public final static int devult_port = 6543;

Protectd Int Port;

Protectd ServersockRt listen_socket;

// Define the wrong case: If there is anything is often wrong, and the procedure is retired.

Public Static Void Fail (Exception E, STRING MSG)

{

System.err.Println (MSG ":" E);

System.exit (1);

}

// Define and launch the Socket case of the server,

Monitor the access request of the passenger machine.

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"; this.Start ();

}

---- / * The main program of the server monitoring thread is under the server. The thread is directly circulated, listening and picking up the contact request from the client. For each connection, a connected pair is generated, and the communication 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)

}

// Launch server main program

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

// Define the Connection class under the following, it is used to handle

The thread of all communication with the client machine.

Class Connection Extends Thread

{

Protected Socket Client;

Protected DataInputStream in;

Protected printstream out;

// initialization of the truncation 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 Streg: E);

Return;

}

THIS.START;

} // End of connectionMETHOD

// Service: Read the text; reverse text; return to 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 * Copy the customer's Java program

// Client.java

Import java.io. *;

Import java.net. *;

Public Class Client Extends

{

Public static final int devult_port = 6543;

/ / Define the wrong case

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;

// Analytical port parameters

IF ((args.length! = 1) && (args.length! = 2) USAGE ();

IF (args.length == 1)

Port = default_port;

Else

{

Try port = integer.parseint (Args [1]);

Catch (Numberformaatexception E) USAGE ();

}

Try {

// Gather a Socket,

Pass through the specified port and host communication.

S = new socket (args [0], port);

// Establish text characters for issuing and receiving

DataInputStream Sin = New DataInputStream (s.getinputStream ());

PrintStream Sout = New DataInputStream (s.getinputStream ());

// Read the character stream from the control desk

DataInputStream in = New DataInputStream (System.in);

// Return to the connected address and port

System.out.println ("Connected to" S.GetinetAddress () ":" S.Getport ());

String line;

For (;;)

{

// Display the indicator

System.out.print (">");

System.out.flush ();

// Read the line of the control station input

Line = in.readline ();

IF (line == null) Break;

// Send the received text line to the server

Sout.Println (line);

// receive a line from the server

Line = sin.readline ();

// Check if connection is closed (i.e. For eof)

IF (line == null)

{

System.out.println ("Connection Closed By Server."); Break;

}

/ / Display the received character on the control station

System.out.println (line);

} // End of for loop

} // end of try

Catch (IOException E) System.err.Println (e);

// Always Be Sure To Close The Socket

Finally

{

TRY IF (s! = null) s.close ();

Catch (IOEXCEPTION E2);

}

} // end of main

} // End of Client

---- Operating the client program, must be used as the first parameter with the server host name, and the server port number is the second parameter, which is the extension port number.

---- Communication address: 276826 Shandong Province, Yellow Haiyi, Japan, Hong Kong, Tongxin, Xinxin, Liu Yang Tel: 0633-8382561MAILTO: liuyang98@163.net

转载请注明原文地址:https://www.9cbs.com/read-3556.html

New Post(0)