The application of Java in Client/server network

Source: Internet
Author: User
Tags file system ftp socket thread

With the growing popularity of the Java language, especially the close integration of Java and the Internet web, it has made a huge global success. The Java language, which is independent of platform, Object-oriented, distributed, multiple clues and perfect security mechanism, has become a good development platform and operating environment in modern information system construction.

Java Network application Model

As with many environments on the Internet, the complete Java application environment is actually a client/server environment, or rather a browser/server model (i.e., the Browser/server model, the Web model, for short). However, unlike the traditional client/server (c/s) Two-tier structure, the Java-based web model is composed of three-tier architecture. The traditional C/s structure through the message transmission mechanism, the client issued a request to the server, the server for the corresponding processing after the delivery mechanism sent back to the client. In the WEB model, one end of the server is broken down into two parts: the Application server (the WEB server) and the other part the database server.

For distributed computing environment, Java provides good support through its network class library. For data distribution, Java provides a URL (uniform Resource Locator) object that allows you to open and Access objects on the network almost exactly as you would access your local file system. For the operation distribution, Java client/server mode can spread the operation from the server to the client side (the server is responsible for providing the results of the query, the client is responsible for the organization of the results of the display), so as to improve the execution efficiency of the whole system, increase dynamic scalability. The Java Network Class library is an extension of the Java language for adapting to an Internet environment. In addition, to adapt to the continuous development of the Internet, Java also provides a dynamic expansion protocol to continuously expand the Java Network Class library.

The Java Network Class library supports a variety of Internet protocols, including Telnet, FTP, and HTTP (WWW), and the corresponding subclass library of the Java Network Class library is:

java.net

Java.net.ftp

Java.net.www.content

Java.net.www.html

Java.net.www.http

These subclass libraries each contain classes and methods that can be used to handle Internet protocols. The java.net is used to handle some basic network functions, including remote logins (telnet), java.net.ftp for handling FTP protocols, and java.net.www.content for processing www page content; Java.net.www.html and Java.net.www.http provide support for HTML languages and HTTP protocols, respectively.

Java applications in client/server environments

The client/server uses a connection based network communication pattern during the distribution process. The communication mode first defines a set of communication protocols between the client and the server, creates a socket class, uses this class to establish a reliable link, and then the client/server transmits the data reliably on the link. The client makes a request, the server listens for requests from the client, and provides a response service to the client. This is the typical "request-answer" pattern. The following is a typical operation of a client/server:

1, the server listens to the corresponding port input;

2, the client issued a request;

3, the server received the request;

4, the server processing the request, and return the results to the client;

5, repeat the above process, until the completion of a session process.

Following these procedures, we write a separate application (application) for servers and clients in the Java language. When the program is on the server, the program listens for client requests, establishes a socket connection for each client request, and provides services to the client. This program provides a service that reads a line of text from the client, reverses the text, and sends it back to the client.

Through this program example, we see that the use of Java language design C/s program needs to pay attention to the following points:

(1), the server should use the ServerSocket class to handle client connection requests. When the client connects to the port on which the server is listening, ServerSocket assigns a new socket object. This new socket object will be connected to a number of new ports, responsible for handling correspondence with the corresponding client. The server then continues to listen for ServerSocket to handle the new client connection.

Sockets and ServerSocket are two classes provided by the Java Network Class library.

(2), the server uses the multithreading mechanism. The server object itself is a thread, and its run () method is an infinite loop to listen for connections from the client. Whenever there is a new client connection, ServerSocket creates a new instance of the socket class, and the server creates a fresh thread, a connection object, that handles the communication based on the socket. All communications with the client are handled by this connection object. The connection constructor initializes the traffic based on the socket object and initiates the running of the thread. Communication with the client and the provision of the service are handled by the connection object.

(3), the client first creates a socket object to communicate with the server. Then you create two objects: DataInputStream and PrintStream, which are used to read data from the socket's InputStream input stream, and the latter to write data to the outputstream of the socket. Finally, the client program reads the data from the standard input (such as the console), writes the data to the server, reads the answer message from the server, and writes the reply messages to the output.

The following is a list of source programs for the server and client side respectively. This program in NT 4.0 network environment (TCP/IP) using JDK1.1 debugging through.

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.