⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 rmi.html

📁 java 基础的 一点东西,,可以看看
💻 HTML
📖 第 1 页 / 共 3 页
字号:
<FONT FACE="Verdana, Arial, Helvetica, sans-serif"><A NAME="compile"></A><H4>Compile the Example</H4> These instructions assume development is in the <CODE>zelda</CODE> home directory.  The server program is compiled in the home directory for user <CODE>zelda</CODE>, but copied to the <CODE>public_html</CODE>directory for user <CODE>zelda</CODE> where it runs.<P>Here is the command sequence forthe Unix and Win32 platforms; an explanation follows.</FONT><PRE><STRONG>Unix:</STRONG>cd /home/zelda/classesjavac Send.javajavac RemoteServer.javajavac RMIClient2.javajavac RMIClient1.javarmic -d . RemoteServercp RemoteServer*.class /home/zelda/public_html/classescp Send.class /home/zelda/public_html/classes</PRE><FONT FACE="Verdana, Arial, Helvetica, sans-serif"></FONT><PRE><STRONG>Win32:</STRONG>cd \home\zelda\classesjavac Send.javajavac RemoteServer.javajavac RMIClient2.javajavac RMIClient1.javarmic -d . RemoteServercopy RemoteServer*.class \home\zelda\public_html\classescopy Send.class \home\zelda\public_html\classes</PRE><FONT FACE="Verdana, Arial, Helvetica, sans-serif">The first two <CODE>javac</CODE> commands compile the<CODE>RemoteServer</CODE> and <CODE>Send</CODE> class andinterface.  The third <CODE>javac</CODE> command compiles the<CODE>RMIClient2</CODE> class. The last <CODE>javac</CODE> command compiles the <CODE>RMIClient1</CODE> class.<P>The next line runs the <CODE>rmic</CODE> command on the <CODE>RemoteServer</CODE> server class. This command produces output class files of the form <CODE>ClassName_Stub.class</CODE>and <CODE>ClassName_Skel.class</CODE>. These output classeslet clients invoke methods on the <CODE>RemoteServer</CODE>server object. <P>The first copy command moves the <CODE>RemoteServer</CODE>class file with its associated <CODE>skel</CODE> and <CODE>stub</CODE> class files to a publicly accessible location in the <CODE>/home/zelda/public_html/classes</CODE>directory, which is on the server machine,so they can be publicly accessed and downloaded. They are placed in the <CODE>public_html</CODE> directory to be under the web server running on the server machine becausethese files are accessed by client programs using URLs.<P>The second copy command moves the <CODE>Send</CODE> class file to the same location for the same reason. The<CODE>RMIClient1</CODE> and <CODE>RMIClient2</CODE> class filesare not made publicly accessible; they communicate from theirclient machines using URLsto access and download the remote object files in the<CODE>public_html</CODE> directory.<UL><LI><FONT FACE="Verdana, Arial, Helvetica, sans-serif"><CODE>RMIClient1</CODE> is invoked from a client-side directoryand uses the server-side web server and client-side Java VM to download the publicly accessible files. </FONT><P><LI><FONT FACE="Verdana, Arial, Helvetica, sans-serif"><CODE>RMIClient2</CODE> is invoked from a client-side directoryand uses the server-side web server and client-side Java VM todownload the publicly accessible files. </FONT></UL><P><IMG SRC="./Art/web.gif" WIDTH="372" HEIGHT="327" ALT=""><A NAME="regis"></A><H4>Start the RMI Registry</H4>Before you start the client programs, you must start theRMI Registry, which is a server-side naming repository that allows remote clients to get a reference to the remote server object. <P>Before you start the RMI Registry, make sure theshell or window in which you run the <CODE>rmiregistry</CODE>command does not have a <CODE>CLASSPATH</CODE> environment variablethat points to the remote object classes, includingthe <CODE>stub</CODE> and <CODE>skel</CODE> classes, anywhereon your system. If the RMI Registry finds these classeswhen it starts, it will not load them from the server-side Java VM, which will create problems when clients try to download the remote server classes.<P><A NAME="registry"></A>The following commands unset the <CODE>CLASSPATH</CODE> and start the RMI Registry on the default1099 port. You can specify a different port byadding the port number as follows:<CODE>rmiregistry 4444 &amp;</CODE>. If you specify a differentport number, you must specify the same port number inyour <A HREF="#port">server-side</A> code as well.</FONT><PRE><STRONG>Unix:</STRONG>cd /home/zelda/public_html/classesunsetenv CLASSPATHrmiregistry &amp;</PRE><PRE><STRONG>Win32:</STRONG>cd \home\zelda\public_html\classesset CLASSPATH=start rmiregistry </PRE><FONT FACE="Verdana, Arial, Helvetica, sans-serif"><BLOCKQUOTE><HR><STRONG>Note:</STRONG> You might want to set the<CODE>CLASSPATH</CODE> back to its originalsetting at this point.<HR></BLOCKQUOTE><A NAME="remote"></A><H4>Run the RemoteServer Server Object</H4>To run the example programs, start <CODE>RemoteServer</CODE>first. If you start either  <CODE>RMIClient1</CODE>or <CODE>RMIClient2</CODE> first, they will not be ableto establish a connection because the remoteserver object is not running.<P>In this example, <CODE>RemoteServer</CODE> is started from the<CODE>/home/zelda/public_html/classes</CODE> directory.<P>The lines beginning at <CODE>java</CODE> should be all onone line with spaces where the lines break. The propertiesspecified with the <CODE>-D</CODE>option to the <CODE>java</CODE> interpreter commandare program attributes that manage the behavior of the programfor this invocation.</FONT><PRE><STRONG>Unix:</STRONG>cd /home/zelda/public_html/classesjava -Djava.rmi.server.codebase=http://kq6py/~zelda/classes-Djava.rmi.server.hostname=kq6py.eng.sun.com-Djava.security.policy=java.policy RemoteServer</PRE><PRE><STRONG>Win32:</STRONG>cd \home\zelda\public_html\classesjava -Djava.rmi.server.codebase=file:       c:\home\zelda\public_html\classes-Djava.rmi.server.hostname=kq6py.eng.sun.com-Djava.security.policy=java.policy RemoteServer</PRE><FONT FACE="Verdana, Arial, Helvetica, sans-serif"><UL><LI><FONT FACE="Verdana, Arial, Helvetica, sans-serif">The <CODE>java.rmi.server.codebase</CODE> property specifieswhere the publicly accessible classes are located.</FONT><P><LI><FONT FACE="Verdana, Arial, Helvetica, sans-serif">The <CODE>java.rmi.server.hostname</CODE> property is thecomplete host name of the server where the publicly accessibleclasses reside.</FONT><P><LI><FONT FACE="Verdana, Arial, Helvetica, sans-serif">The <CODE>java.rmi.security.policy</CODE> property specifiesthe <A HREF="./Code/java.policy">policy file</A>with the permissions needed to run the remote server object andaccess the remote server classes for download.</FONT><P><LI><FONT FACE="Verdana, Arial, Helvetica, sans-serif">The class to execute (<CODE>RemoteServer</CODE>).</FONT></UL><A NAME="1"></A><H4>Run the RMIClient1 Program</H4>Here is the command sequence for the Unix and Win32 platforms;an explanation follows.<P>In this example, <CODE>RMIClient1</CODE> is started from the<CODE>/home/zelda/classes</CODE> directory.<P>The lines beginning at <CODE>java</CODE> should be all onone line with spaces where the lines break.Properties specified with the<CODE>-D</CODE> option to the <CODE>java</CODE> interpretercommand are program attributes that manage the behavior of the programfor this invocation.</FONT><PRE><STRONG>Unix:</STRONG>cd /home/zelda/classesjava -Djava.rmi.server.codebase=                         http://kq6py/~zelda/classes/-Djava.security.policy=java.policy                   RMIClient1 kq6py.eng.sun.com</PRE><PRE><STRONG>Win32:</STRONG>cd \home\zelda\classesjava -Djava.rmi.server.codebase=                         file:c:\home\zelda\classes\-Djava.security.policy=java.policy                          RMIClient1 kq6py.eng.sun.com</PRE><FONT FACE="Verdana, Arial, Helvetica, sans-serif"><UL><LI><FONT FACE="Verdana, Arial, Helvetica, sans-serif">The <CODE>java.rmi.server.codebase</CODE> property specifieswhere the publicly accessible classes for downloading are located.</FONT><P><LI><FONT FACE="Verdana, Arial, Helvetica, sans-serif">The <CODE>java.security.policy</CODE> property specifiesthe <A HREF="./Code/java.policy">policy file</A>with the permissions needed to run the client program and accessthe remote server classes.</FONT><P><LI><FONT FACE="Verdana, Arial, Helvetica, sans-serif">The client program class to execute (<CODE>RMIClient1</CODE>),and the host name of the server (<CODE>Kq6py</CODE>) wherethe remote server classes are.</FONT></UL><A NAME="2"></A><H4>Run RMIClient2</H4>Here is the command sequence for the Unix and Win32platforms; an explanation follows.<P>In this example, <CODE>RMIClient2</CODE> is started from the<CODE>/home/zelda/classes</CODE> directory.<P>The lines beginning at <CODE>java</CODE> should be all onone line with spaces where the lines break. The propertiesspecified with the <CODE>-D</CODE>option to the <CODE>java</CODE> interpreter commandare program attributes that manage the behavior of the programfor this invocation.</FONT><PRE><STRONG>Unix:</STRONG>cd /home/zelda/classesjava -Djava.rmi.server.codebase=                         http://kq6py/~zelda/classes-Djava.security.policy=java.policy                          RMIClient2 kq6py.eng.sun.com</PRE><PRE><STRONG>Win32:</STRONG>cd \home\zelda\classesjava -Djava.rmi.server.codebase=                file:c:\home\zelda\public_html\classes-Djava.security.policy=java.policy                          RMIClient2 kq6py.eng.sun.com</PRE><FONT FACE="Verdana, Arial, Helvetica, sans-serif"><UL><LI><FONT FACE="Verdana, Arial, Helvetica, sans-serif">The <CODE>java.rmi.server.codebase</CODE> property specifieswhere the publicly accessible classes are located.</FONT><P><LI><FONT FACE="Verdana, Arial, Helvetica, sans-serif">The <CODE>java.rmi.server.hostname</CODE> property is thecomplete host name of the server where the publicly accessibleclasses reside.</FONT><P><LI><FONT FACE="Verdana, Arial, Helvetica, sans-serif">The <CODE>java.rmi.security.policy</CODE> property specifiesthe <A HREF="./Code/java.policy">policy file</A>with the permissions needed to run the remote server object andaccess the remote server classes for download.</FONT><P><LI><FONT FACE="Verdana, Arial, Helvetica, sans-serif">The class to execute (<CODE>RMIClient2</CODE>).</FONT></UL><A NAME="send"></A><H3>RemoteServer Class</H3>The <A HREF="./Code/RemoteServer.java">RemoteServer</A> class extends <CODE>UnicastRemoteObject</CODE> and implements the<CODE>sendData</CODE> and <CODE>getData</CODE> methodsdeclared in the <CODE>Send</CODE> interface. Theseare the remotely accessible methods.<P><CODE>UnicastRemoteObject</CODE> implements a number of<CODE>java.lang.Object</CODE> methods for remote objectsand includes constructors and static methods to makea remote object available to receive method calls fromclient programs.</FONT><PRE>class RemoteServer extends UnicastRemoteObject                 implements Send {  String text;  public RemoteServer() throws RemoteException {    super();  }  public void sendData(String gotText){    text = gotText;  }  public String getData(){    return text;  }</PRE><FONT FACE="Verdana, Arial, Helvetica, sans-serif">The <CODE>main</CODE> method installs the<CODE>RMISecurityManager</CODE> and opensa connection with a port on the machinewhere the server program runs.The security manager determines whether there is a policy filethat lets downloaded code perform tasks that require permissions.<A NAME="port"></A>The <CODE>main</CODE> method creates a name for the

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -