📄 rmi.html
字号:
the <CODE>RemoteServer</CODE> object that includes theserver name (<CODE>kq6py</CODE>) where the RMI Registryand remote object run, and the name, <CODE>Send</CODE>.<P>By default the server name uses port 1099. If youwant to use a different port number, you can add itwith a colon as follows: <CODE>kq6py:4444</CODE>.If you change the port here, you must start the<A HREF="#registry">RMI Registry</A> with the same port number.<P>The <CODE>try</CODE> block creates an instance ofthe <CODE>RemoteServer</CODE> class and binds the <CODE>name</CODE>to the remote object to the RMI Registry with the<CODE>Naming.rebind(name, remoteServer);</CODE> statement.</FONT><PRE> public static void main(String[] args){ if(System.getSecurityManager() == null) { System.setSecurityManager(new RMISecurityManager()); } String name = "//kq6py.eng.sun.com/Send"; try { Send remoteServer = new RemoteServer(); Naming.rebind(name, remoteServer); System.out.println("RemoteServer bound"); } catch (java.rmi.RemoteException e) { System.out.println("Cannot create remote server object"); } catch (java.net.MalformedURLException e) { System.out.println("Cannot look up server object"); } }}</PRE><FONT FACE="Verdana, Arial, Helvetica, sans-serif"><BLOCKQUOTE><HR><STRONG>Note:</STRONG>The <CODE>remoteServer</CODE> object is type<CODE>Send</CODE> (see instance declaration at top of class)because the interface available to clients is the<CODE>Send</CODE> interface and its methods;not the <CODE>RemoteServer</CODE> class and itsmethods.<HR></BLOCKQUOTE><A NAME="int"></A><H3>Send Interface</H3>The <A HREF="./Code/Send.java">Send</A> interface declares themethods implemented in the <CODE>RemoteServer</CODE>class. These are the remotely accessible methods.</FONT><PRE>public interface Send extends Remote { public void sendData(String text) throws RemoteException; public String getData() throws RemoteException;}</PRE><FONT FACE="Verdana, Arial, Helvetica, sans-serif"><A NAME="first"></A><H3>RMIClient1 Class</H3>The <A HREF="./Code/RMIClient1.java">RMIClient1</A> class establishesa connection to the remote server program and sendsdata to the remote server object. The code to do these thingsis in the <CODE>actionPerformed</CODE> and <CODE>main</CODE> methods.<H4>actionPerformed Method</H4>The <CODE>actionPerformed</CODE> method calls the<CODE>RemoteServer.sendData</CODE> method to sendtext to the remote server object.</FONT><PRE>public void actionPerformed(ActionEvent event){ Object source = event.getSource(); if(source == button){//Send data over socket String text = textField.getText(); try{ send.sendData(text); } catch (java.rmi.RemoteException e) { System.out.println("Cannot send data to server"); } textField.setText(new String("")); }}</PRE><FONT FACE="Verdana, Arial, Helvetica, sans-serif"><H4>main Method</H4>The <CODE>main</CODE> method installs the <CODE>RMISecurityManager</CODE> and creates a <CODE>name</CODE> to use to look up the <CODE>RemoteServer</CODE> server object. The client usesthe <CODE>Naming.lookup</CODE> method to look upthe <CODE>RemoteServer</CODE> object in the RMI Registry running on the server. <P>The security manager determines whether there is a policy filethat lets downloaded code perform tasks that require permissions.</FONT><PRE> RMIClient1 frame = new RMIClient1(); if(System.getSecurityManager() == null) { System.setSecurityManager(new RMISecurityManager()); } try {//args[0] contains name of server where Send runs String name = "//" + args[0] + "/Send"; send = ((Send) Naming.lookup(name)); } catch (java.rmi.NotBoundException e) { System.out.println("Cannot look up remote server object"); } catch(java.rmi.RemoteException e){ System.out.println("Cannot look up remote server object"); } catch(java.net.MalformedURLException e) { System.out.println("Cannot look up remote server object"); }</PRE><FONT FACE="Verdana, Arial, Helvetica, sans-serif"><A NAME="second"></A><H3>RMIClient2 Class</H3>The <A HREF="./Code/RMIClient2.java">RMIClient2</A> class establishes a connection with the remote server program and gets the data from the remote server objectand displays it. The code to do this is in the<CODE>actionPerformed</CODE> and <CODE>main</CODE> methods.<H4>actionPerformed Method</H4>The <CODE>actionPerformed</CODE> method calls the<CODE>RemoteServer.getData</CODE> method to retrievethe data sent by the client program.This data is appended to the <CODE>TextArea</CODE>object for display to the end user on the server side.</FONT><PRE>public void actionPerformed(ActionEvent event) { Object source = event.getSource(); if(source == button){ try{ String text = send.getData(); textArea.append(text); } catch (java.rmi.RemoteException e) { System.out.println("Cannot send data to server"); } } }}</PRE><FONT FACE="Verdana, Arial, Helvetica, sans-serif"><H4>main Method</H4>The <CODE>main</CODE> method installs the<CODE>RMISecurityManager</CODE> and creates a<CODE>name</CODE> to use to look up the<CODE>RemoteServer</CODE> server object. The<CODE>args[0]</CODE> parameter provides thename of the server host. The client usesthe <CODE>Naming.lookup</CODE> method to look upthe <CODE>RemoteServer</CODE> object in theRMI Registry running on the server.<P>The security manager determines whether there is a policy filethat lets downloaded code perform tasks that require permissions.</FONT><PRE> RMIClient2 frame = new RMIClient2(); if(System.getSecurityManager() == null) { System.setSecurityManager(new RMISecurityManager()); } try { String name = "//" + args[0] + "/Send"; send = ((Send) Naming.lookup(name)); } catch (java.rmi.NotBoundException e) { System.out.println("Cannot look up remote server object"); } catch(java.rmi.RemoteException e){ System.out.println("Cannot look up remote server object"); } catch(java.net.MalformedURLException e) { System.out.println("Cannot look up remote server object"); }</PRE><FONT FACE="Verdana, Arial, Helvetica, sans-serif"><A NAME="more"></A><H3>More Information</H3>You can find more information on the RMI API in the <A HREF="http://java.sun.com/docs/books/tutorial/rmi/index.html">RMI</A> trail of <A HREF="http://java.sun.com/docs/books/tutorial/index.html">TheJava Tutorial</A>.<P ALIGN="RIGHT"><FONT SIZE="-1">[<A HREF="#top">TOP</A>]</FONT></FONT></TD></TR></TABLE><!-- ================ --><!-- End Main Content --><!-- ================ --></FONT></TD></TR></TABLE><!-- Copyright Insert --><BR CLEAR="ALL"><FORM ACTION="/cgi-bin/search.cgi" METHOD="POST"><TABLE WIDTH="100%" CELLPADDING="0" BORDER="0" CELLSPACING="5"> <TR> <TD VALIGN="BOTTOM"></TD></TR><A HREF="/servlet/PrintPageServlet"><IMG SRC="/images/printbutton.gif" WIDTH="155" HEIGHT="25" ALT="Print Button" BORDER="0"></A> <CENTER> <FONT SIZE="-1" COLOR="#999999" FACE="Verdana, Arial, Helvetica, sans-serif"> [ This page was updated: <!-- new date --> 31-Mar-2000 ]</font></CENTER> </TD> </TR> <TR> <TD BGCOLOR="#CCCCCC"> <IMG SRC="/images/pixel.gif" HEIGHT="1" WIDTH="1" ALT=""></TD> </TR> <TR> <TD> <CENTER> <FONT SIZE="-2" FACE="Verdana, Arial, Helvetica, sans-serif"> <A HREF="http://java.sun.com/products/">Products & APIs</A> | <A HREF="/developer/index.html">Developer Connection</A> | <A HREF="/developer/infodocs/index.shtml">Docs & Training</A> | <A HREF="/developer/support/index.html">Online Support</A><BR> <A HREF="/developer/community/index.html">Community Discussion</A> | <A HREF="http://java.sun.com/industry/">Industry News</A> | <A HREF="http://java.sun.com/solutions">Solutions Marketplace</A> | <A HREF="http://java.sun.com/casestudies">Case Studies</A> </FONT> </CENTER> </TD> </TR> <TR> <TD BGCOLOR="#CCCCCC"> <IMG SRC="/images/pixel.gif" HEIGHT="1" WIDTH="1" ALT=""></TD> </TR> <TR> <TD ALIGN="CENTER"> <FONT SIZE="-2" FACE="Verdana, Arial, Helvetica, sans-serif"> <A HREF="http://java.sun.com/docs/glossary.html">Glossary</A> - <A HREF="http://java.sun.com/applets/">Applets</A> - <A HREF="http://java.sun.com/docs/books/tutorial/">Tutorial</A> - <A HREF="http://java.sun.com/jobs/">Employment</A> - <A HREF="http://java.sun.com/nav/business/">Business & Licensing</A> - <A HREF="http://java.sun.com/javastore/">Java Store</A> - <A HREF="http://java.sun.com/casestudies/">Java in the Real World</A> </FONT> </TD> </TR> <TR> <TD> <CENTER> <FONT SIZE="-2" FACE="Verdana, Arial, Helvetica, sans-serif"> <a href="/siteinfo/faq.html">FAQ</a> | <a href="/feedback/index.html">Feedback</a> | <a href="http://www.dynamicdiagrams.net/mapa/cgi-bin/help.tcl?db=javasoft&dest=http://java.sun.com/">Map</a> | <A HREF="http://java.sun.com/a-z/index.html">A-Z Index</A> </FONT> </CENTER> </TD> </TR> <TR> <TD> <TABLE WIDTH="100%" CELLPADDING="0" BORDER="0" CELLSPACING="0"> <TR> <TD WIDTH="50%"> <FONT SIZE="-2" FACE="Verdana, Arial, Helvetica, sans-serif"> For more information on Java technology<BR> and other software from Sun Microsystems, call:<BR> </FONT> <FONT SIZE="-1" FACE="Verdana, Arial, Helvetica, sans-serif"> (800) 786-7638<BR></FONT> <FONT SIZE="-2" FACE="Verdana, Arial, Helvetica, sans-serif"> Outside the U.S. and Canada, dial your country's <A HREF="http://www.att.com/business_traveler/attdirecttollfree/">AT&T Direct Access Number</A> first.<BR> </FONT> </TD> <TD ALIGN="RIGHT" WIDTH="50%"> <A HREF="http://www.sun.com"><IMG SRC="/images/lgsun.gif" width="64" height="30" border="0" ALT="Sun Microsystems, Inc."></A><BR> <FONT SIZE="-2" FACE="Verdana, Arial, Helvetica, sans-serif"> Copyright © 1995-2000 <A HREF="http://www.sun.com">Sun Microsystems, Inc.</A><BR> All Rights Reserved. <A HREF="http://www.sun.com/share/text/termsofuse.html">Terms of Use</A>. <A HREF="http://www.sun.com/privacy/">Privacy Policy</A>. </FONT> </TD> </TR> </TABLE> </TD> </TR> </TABLE></FORM><!-- End Copyright Insert --></BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -