📄 rmi.html
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"><HTML><HEAD> <TITLE>Java(TM) Language Basics, Part 1, Lesson 8: Remote Method Invocation</TITLE> <META NAME="AUTHOR" CONTENT="Monica Pawlan"> <META NAME="KEYWORDS" CONTENT="programming, basics, Java 2"> <META NAME="OWNER" CONTENT="Editorial/JDC"> <META NAME="revision" CONTENT="@(#)rmi.src 1.26 03/09/00 JDC"> <STYLE TYPE="text/css"> <!-- CODE {font-family: Courier, Monospace; font-size: 12pt} PRE {font-family: Courier, Monospace; font-size: 11pt} --> </STYLE></HEAD><!-- Start Body Insert--><BODY BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#0000FF" ALINK="#FF0000" VLINK="#660066"><!-- End Body Insert--><!-- Start PageTop Insert --><TABLE BORDER="0" CELLSPACING="0" CELLPADDING="0" WIDTH="100%"> <TR ALIGN="CENTER" VALIGN="TOP"> <TD WIDTH="157" ALIGN="LEFT"> <IMG SRC="/images/pixel.gif" HEIGHT="40" WIDTH="40" ALT=""> <A HREF="http://java.sun.com/index.html"><IMG SRC="/images/javalogo52x88.gif" WIDTH="52" HEIGHT="88" ALT="Java Technology Home Page" BORDER="0"></A> <BR> <IMG SRC="/images/pixel.gif" WIDTH="157" HEIGHT="1" ALT=""></TD> <TD> <FORM NAME="seek1" METHOD="GET" ACTION="http://search.java.sun.com/query.html"> <TABLE BORDER="0" CELLPADDING="0" CELLSPACING="0" WIDTH="100%"> <TR> <TD ALIGN="RIGHT"> <IMG SRC="/images/stripelt.gif" WIDTH="6" HEIGHT="14" ALT=""></TD> <TD WIDTH="100%"> <TABLE BORDER="0" CELLPADDING="0" CELLSPACING="0" WIDTH="100%"> <TR> <TD BGCOLOR="#CC9966" WIDTH="100%"> <IMG SRC="/images/pixel.gif" HEIGHT="2" WIDTH="1" ALT=""></TD> </TR> <TR> <TD> <IMG SRC="/images/pixel.gif" HEIGHT="2" WIDTH="1" ALT=""></TD> </TR> <TR> <TD BGCOLOR="#CC9966"> <IMG SRC="/images/pixel.gif" HEIGHT="2" WIDTH="1" ALT=""></TD> </TR> <TR> <TD> <IMG SRC="/images/pixel.gif" HEIGHT="2" WIDTH="1" ALT=""></TD> </TR> <TR> <TD BGCOLOR="#CC9966"> <IMG SRC="/images/pixel.gif" HEIGHT="2" WIDTH="1" ALT=""></TD> </TR> <TR> <TD> <IMG SRC="/images/pixel.gif" HEIGHT="2" WIDTH="1" ALT=""></TD> </TR> <TR> <TD BGCOLOR="#CC9966"> <IMG SRC="/images/pixel.gif" HEIGHT="2" WIDTH="1" ALT=""></TD> </TR> </TABLE> </TD> <TD ALIGN="LEFT"> <IMG SRC="/images/stripert.gif" WIDTH="6" HEIGHT="14" ALT=""></TD> <TD> <TABLE BORDER="0" CELLPADDING="0" CELLSPACING="0"> <TR> <TD VALIGN="CENTER"> <A HREF="http://java.sun.com/a-z/index.html"><IMG SRC="/images/azindex.gif" BORDER="0" WIDTH="72" HEIGHT="11" ALT="A-Z Index"></A></TD> <TD VALIGN="CENTER"> <FONT FACE="Helvetica" SIZE="1"> <INPUT TYPE="text" SIZE="15" MAXLENGTH="128" NAME=qt></FONT></TD> <TD VALIGN="CENTER"> <INPUT TYPE="image" SRC="/images/search.button.gif" WIDTH="55" HEIGHT="14" ALT="Search" value="search" BORDER="0"></TD> </TR> </TABLE> </TD> </TR> </TABLE> </FORM> <P> <TABLE BORDER="0" WIDTH="100%" CELLPADDING="0" CELLSPACING="0"> <TR VALIGN="TOP"> <TD WIDTH="100%"><IMG SRC="/images/chiclet.row.gif" WIDTH="55" HEIGHT="18" ALT=""></TD> <TD ROWSPAN="4" ALIGN="RIGHT" WIDTH="152"> <A HREF="/developer/index.html"><IMG SRC="/images/developer.connection.header.gif" BORDER="0" HEIGHT="42" WIDTH="319" ALT="Java Developer Connection(SM)"></A></TD> </TR> <TR VALIGN="TOP"> <TD BGCOLOR="#FFFFFF" HEIGHT="1" WIDTH="100%"> <IMG SRC="/images/pixel.gif" HEIGHT="1" WIDTH="1" ALT=""></TD> </TR> <TR VALIGN="TOP"> <TD BGCOLOR="#CC9966" HEIGHT="1" WIDTH="100%"> <IMG SRC="/images/pixel.gif" HEIGHT="1" WIDTH="1" ALT=""></TD> </TR> <TR VALIGN="TOP"> <TD><A HREF="/developer/onlineTraining/"><IMG SRC="/images/online-training.gif" WIDTH="165" HEIGHT="22" ALT="Online Training" BORDER=0></A></TD> </TR> </TABLE> </TD> </TR></TABLE><!-- End PageTop Insert --><!-- Start NavBar Insert --><TABLE BORDER="0" CELLSPACING="0" CELLPADDING="3" BGCOLOR="#FFFFFF" WIDTH="157" ALIGN="LEFT"><!-- tab categories --><TR><TD><A HREF="http://java.sun.com/products/"><IMG SRC="/images/side.tab.products.gif" HEIGHT="15" WIDTH="130" BORDER="0" ALT="Downloads, APIs, Documentation"></A></TD></TR><TR><TD><A HREF="/developer/index.html"><IMG SRC="/images/side.tab.developer.gif" HEIGHT="15" WIDTH="130" BORDER="0" ALT="Java Developer Connection"></A></TD></TR><TR><TD><A HREF="/developer/infodocs/index.shtml"><IMG SRC="/images/side.tab.docs.gif" HEIGHT="15" WIDTH="130" BORDER="0" ALT="Tutorials, Tech Articles, Training"></A></TD></TR><TR><TD><A HREF="/developer/support/index.html"><IMG SRC="/images/side.tab.support.gif" HEIGHT="15" WIDTH="130" BORDER="0" ALT="Online Support"></A></TD></TR><TR><TD><A HREF="/developer/community/index.html"><IMG SRC="/images/side.tab.community.gif" HEIGHT="15" WIDTH="130" BORDER="0" ALT="Community Discussion"></A></TD></TR><TR><TD><A HREF="http://java.sun.com/industry/"><IMG SRC="/images/side.tab.news.gif" HEIGHT="15" WIDTH="130" BORDER="0" ALT="News & Events from Everywhere"></A></TD></TR><TR><TD><A HREF="http://java.sun.com/solutions"><IMG SRC="/images/side.tab.solutions.gif" HEIGHT="15" WIDTH="130" BORDER="0" ALT="Products from Everywhere"></A></TD></TR><TR><TD><A HREF="http://java.sun.com/casestudies"><IMG SRC="/images/side.tab.case.gif" HEIGHT="15" WIDTH="130" BORDER="0" ALT="How Java Technology is Used Worldwide"></A></TD></TR><TR><TD> <DIV ALIGN="RIGHT"> <A HREF="/servlet/PrintPageServlet"><IMG SRC="/images/printbutton.gif" WIDTH="155" HEIGHT="25" ALT="Print Button" BORDER="0"></A> </DIV></TD></TR><TR><TD> </TD></TR><!-- End NavBar Insert --><!-- START SUB-NAV --><TR> <TD><!-- INSERT SUB-NAV INFO --> </TD></TR><!-- END SUB-NAV --></TABLE><TABLE BORDER="0" CELLPADDING="0" CELLSPACING="0"><TR><TD><FONT FACE="Verdana, Arial, Helvetica, sans-serif"><!-- Template Version 2.0 --><!-- ================== --><!-- Start Main Content --><!-- ================== --><TABLE BORDER="0" CELLPADDING="0" CELLSPACING="0"><TR><TD VALIGN="TOP"><FONT FACE="Verdana, Arial, Helvetica, sans-serif"> <A NAME="top"></A> <DIV ALIGN="RIGHT"> <FONT SIZE="-1"><A HREF="/developer/onlineTraining/index.html">Training Index</A></FONT> <H2>Java<FONT SIZE="-2"><SUP>TM</SUP></FONT> Programming Language Basics, Part 1<BR> <FONT SIZE="3">Lesson 8: Remote Method Invocation</FONT></H2> <FONT SIZE="-1">[<A HREF="dba.html"><<BACK</A>] [<A HREF="index.html#contents">CONTENTS</A>] [<A HREF="end.html">NEXT>></A>]</FONT></DIV><P>The Java<FONT SIZE="-2"><SUP>TM</SUP></FONT> Remote Method Invocation (RMI) applicationprogramming interface (API) enables client and servercommunications over the net. Typically, clientprograms send requests to a server program, and the serverprogram responds to those requests.<P>A common example is sharing a word processing programover a network. The word processor is installed on a server, and anyone who wants to use it starts it from his or her machine by double clicking an icon on the desktop or typing at the command line.The invocation sends a request to a server programfor acess to the software, and the server program respondsby making the software available to the requestor.<P><IMG SRC ="./Art/remote.gif" WIDTH="232" HEIGHT="97" ALT="" ALIGN="LEFT" HSPACE=10 VSPACE=10>The RMI API lets you create a publicly accessible remote server object that enables client and server communications through simplemethod calls on the server object. Clients can easily communicatedirectly with the server object and indirectly with each other through the server object using Uniform Resource Locators (URLs) andHyperText Transfer Protocol (HTTP). <P>This lesson explains how to use the RMI API to establish client andserver communications. <UL><LI><FONT FACE="Verdana, Arial, Helvetica, sans-serif"><A HREF="#about">About the Example</A></FONT><UL> <LI><FONT FACE="Verdana, Arial, Helvetica, sans-serif"> <A HREF="#behave">Program Behavior</A></FONT> <LI><FONT FACE="Verdana, Arial, Helvetica, sans-serif"> <A HREF="#file">File Summary</A></FONT> <LI><FONT FACE="Verdana, Arial, Helvetica, sans-serif"> <A HREF="#compile">Compile the Example</A></FONT> <LI><FONT FACE="Verdana, Arial, Helvetica, sans-serif"> <A HREF="#regis">Start the RMI Registry</A></FONT> <LI><FONT FACE="Verdana, Arial, Helvetica, sans-serif"> <A HREF="#remote">Run the RemoteServer Server Object</A></FONT> <LI><FONT FACE="Verdana, Arial, Helvetica, sans-serif"> <A HREF="#1">Run the RMIClient1 Program</A></FONT> <LI><FONT FACE="Verdana, Arial, Helvetica, sans-serif"> <A HREF="#2">Run the RMIClient2 Program</A></FONT></UL><LI><FONT FACE="Verdana, Arial, Helvetica, sans-serif"><A HREF="#send">RemoteServer Class</A></FONT><LI><FONT FACE="Verdana, Arial, Helvetica, sans-serif"><A HREF="#int">Send Interface</A></FONT><LI><FONT FACE="Verdana, Arial, Helvetica, sans-serif"><A HREF="#first">RMIClient1 Class</A></FONT><LI><FONT FACE="Verdana, Arial, Helvetica, sans-serif"><A HREF="#second">RMIClient2 Class</A></FONT><LI><FONT FACE="Verdana, Arial, Helvetica, sans-serif"><A HREF="#more">More Information</A></FONT></UL><HR><A NAME="about"></A><H3>About the Example</H3>This lesson converts the<A HREF="./Code/FileIO.java">File Input and Output</A>application from <A HREF="data.html">Lesson 6: File Accessand Permissions</A> to the RMI API.<A NAME="behave"></A><H4>Program Behavior</H4>The <A HREF="./Code/RMIClient1.java">RMIClient1</A> program presentsa simple user interface and prompts for textinput. When you click the <CODE>Click Me</CODE>button, the text is sent to the <A HREF="./Code/RMIClient2.java">RMIClient2</A> programby way of the remote server object.When you click the <CODE>Click Me</CODE> button on the<CODE>RMIClient2</CODE> program, the text sent from <CODE>RMIClient1</CODE> appears.<P><IMG SRC="./Art/client1a.gif" WIDTH="199" HEIGHT="129" ALT=""><IMG SRC="./Art/client2a.gif" WIDTH="199" HEIGHT="131" ALT=""><P><DIV ALIGN="LEFT">First Instance of Client 1</DIV><P>If you start a second instance of <CODE>RMIClient1</CODE> and type in some text, that text is sent to <CODE>RMIClient2</CODE>when you click the <CODE>Click Me</CODE> button. To see the text received by <CODE>RMIClient2</CODE>, click its <CODE>Click Me</CODE> button. <P><IMG SRC="./Art/client1b.gif" WIDTH="199" HEIGHT="129" ALT=""><IMG SRC="./Art/client2b.gif" WIDTH="199" HEIGHT="131" ALT=""><P><DIV ALIGN="LEFT">Second Instance of Client 1</DIV><P><A NAME="file"></A><H4>File Summary</H4>The example program consists of the RMIClient1 program, remote object and interface, and the RMIClient2 programas illustrated in the diagram. The correspondingsource code files for these executables are describedin the bullet list below.<P><IMG SRC="./Art/rmi.gif" WIDTH="394" HEIGHT="161" ALT=""><UL><LI><FONT FACE="Verdana, Arial, Helvetica, sans-serif"><A HREF="./Code/RMIClient1.java">RMIClient1.java</A>:Client program that calls the <CODE>sendData</CODE>method on the <CODE>RemoteServer</CODE> server object.</FONT><P><LI><FONT FACE="Verdana, Arial, Helvetica, sans-serif"><A HREF="./Code/RMIClient2.java">RMIClient2.java</A>:Client program that calls the <CODE>getData</CODE>method on the <CODE>RemoteServer</CODE> server object.</FONT><P><LI><FONT FACE="Verdana, Arial, Helvetica, sans-serif"><A HREF="./Code/RemoteServer.java">RemoteServer.java</A>:Remote server object that implements <CODE>Send.java</CODE> andthe <CODE>sendData</CODE> and <CODE>getData</CODE>remote methods.</FONT><P><LI><FONT FACE="Verdana, Arial, Helvetica, sans-serif"><A HREF="./Code/Send.java">Send.java</A>:Remote interface that declares the <CODE>sendData</CODE> and<CODE>getData</CODE> remote server methods.</FONT></UL>In addition, the following <A HREF="./Code/java.policy">java.policy</A>security policy file grants the permissions needed to run the example. </FONT><PRE>grant { permission java.net.SocketPermission "*:1024-65535", "connect,accept,resolve"; permission java.net.SocketPermission "*:80", "connect"; permission java.awt.AWTPermission "accessEventQueue"; permission java.awt.AWTPermission "showWindowWithoutWarningBanner";};</PRE>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -