Java rmi server client data transmission instance tutorial, rmi instance tutorial

Source: Internet
Author: User

Java rmi server client data transmission instance tutorial, rmi instance tutorial

Original article: java rmi server client data transmission instance tutorial java code: http://www.zuidaima.com/share/1550463269620736.htm

Java rmi server client data transmission instance tutorial

Package com. zuidaima. rmi; import java. rmi. *;/*** @ author www.zuidaima.com **/public class ZuidaimaClient {public static void main (String [] args) {// set the security mechanism on the server side/* if (System. getSecurityManager () = null) {System. setSecurityManager (new RMISecurityManager ();} * // * the default host and default port */String host = "www.zuidaima.com: 1199";/* When parameters are input, set host to the specified host */if (args. length> 0) host = args [0]; try {/* locate the remote implementation object based on the specified URL * // * "h" is an identifier, we will use it to point to the remote object implementing the "Hello" interface */System. out. println (host + "*****"); Hello h = (Hello) Naming. lookup ("rmi: //" + host + "/HelloServer"); System. out. println ("remote object implementing the" Hello "interface:" + h); System. out. println ("On the client, I started to call the 'sayhello' method on the RMI server"); System. out. println ("Welcome," + h. sayHello ("www.zuidaima.com");} catch (Exception ex) {System. out. println ("error" + ex );}}}

 




How does RMI implement synchronous data transmission?

Remote Method Invocation (RMI) is the pillar of Enterprise JavaBeans and a convenient way to build distributed Java applications. RMI is very easy to use, but it is very powerful.

The foundation of RMI is interfaces. the RMI architecture is based on an important principle: the specific implementation of defining interfaces and defining interfaces is separated. The following example shows how to create a simple Remote Computing Service and a customer program that uses it.

A normally working RMI system consists of the following parts:

● Remote service interface definition

● Specific implementation of remote service interfaces

● Pile (Stub) and framework (Skeleton) files

● A server running remote services

● An RMI naming service that allows the client to discover this remote service

● File provider (an HTTP or FTP Server)

● A client program that requires this remote service

Next we will build a simple RMI system step by step. First, create a new folder on your machine to store the files we created. For simplicity, we only use one folder to store the client and server code, and run the server and client in the same directory.

If all the RMI files have been designed, you need to take the following steps to generate your system:

1. Compile and compile the Java code of the interface

2. Compile and compile the Java code implemented by the interface

3. Generate Stub and Skeleton files from the interface implementation class.

4. Compile the master program of the remote service

5. Write the RMI client program

6. Install and run the RMI system

1. Interface

The first step is to establish and compile the Java code of the service interface. This interface defines all functions that provide remote services. The following is the source program:

// Calculator. java
// Define the interface
Import java. rmi. Remote;

Public interface Calculator extends Remote
{
Public long add (long a, long B)
Throws java. rmi. RemoteException;

Public long sub (long a, long B)
Throws java. rmi. RemoteException;

Public long mul (long a, long B)
Throws java. rmi. RemoteException;

Public long div (long a, long B)
Throws java. rmi. RemoteException;
}
Note that this interface is inherited from Remote. Each defined method must throw a RemoteException object.

Create this file, store it in the directory just now, and compile it.

> Javac Calculator. java

2. Specific implementation of interfaces

Next, we need to write the specific implementation of the remote service. This is a CalculatorImpl file:

// CalculatorImpl. java
// Implementation
Import java. rmi. server. UnicastRemoteObject

Public class CalculatorImpl extends UnicastRemoteObject implements ...... the remaining full text>
 
How does java implement data transmission between servers and customer service terminals?

If you have learned the basics of the network, you should know the popular TCP/IP protocol. Java supports operations at the network layer or above.
There are many transfer methods. There are RMI or Socket methods in Java SE (tcp and udp ). These are all C/S methods.
In addition, Java EE uses the B/S method to initiate requests from the client to the servlet/jsp on the server. The underlying layer uses the http protocol and an application layer protocol.
In fact, other protocols at the application layer, such as ftp, telnet, and even webservice, can be implemented in Java and selected as needed. Generally, when learning the basics of Java, we use socket programming to develop tcp or udp communication programs. Other methods are basically based on this method, which is more suitable for the corresponding fields.

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.