服务器端程序:
import java.rmi.Remote;
import java.rmi.RemoteException;public interface Arith extends Remote {
int[] add(int []a,int []b)throws RemoteException;
}/*******************************************************************************/
import java.rmi.server.UnicastRemoteObject;
import java.rmi.*;
import java.rmi.registry.*;public class ArithImpl extends UnicastRemoteObject implements Arith{
private static final long serialVersionUID = 1124794857849355931L;
    private String objectName;public ArithImpl(String objectname)throws RemoteException
{
super();
this.objectName = objectname;
}
/**
 * a.length == b.length;
 */
public int[]add(int []a,int []b)
{
for(int i = 0 ;i<a.length;i++)
a[i]+=b[i]; 
return a;
}
public static void main(String[] args) {

 if(System.getSecurityManager() == null) {
         System.setSecurityManager(new RMISecurityManager());
      }    Registry reg;
    try{
     ArithImpl obj = new ArithImpl("ArithServer");
     //Naming.rebind("ArithServer",obj);
     System.out.println("//hostname/ArithServer bound in registry!");
        try{
         reg = LocateRegistry.createRegistry(1099);
         reg.rebind(obj.objectName,(Arith)obj);
         System.out.println("###################################");
        }catch(RemoteException e){
         e.printStackTrace();
        }
    }catch(Exception e)
    {
     System.out.println("ArithImpl error:"+e.getMessage());
     e.printStackTrace();
    }
}
}
运行结果://hostname/ArithServer bound in registry!
###################################客户端程序:
import java.rmi.*;
import java.net.*;
public class ArithApp { /**
 * @param args
 */
public static void main(String[] args) {
// TODO Auto-generated method stub
int []a = {1,2,3,4,5,6,7,8,9};
int []b = {1,2,3,4,5,6,7,8,9};int [] result ;try{
Arith obj = (Arith)Naming.lookup("//localhost/ArithServer");
result = obj.add(a,b);
System.out.println("The sun is:");
ArithApp.show(result);
}catch(Exception e)
{
System.out.println("ArithApp:"+e.getMessage());
e.printStackTrace();
}
}
public static void show(int[] a )
{
for(int i = 0;i<a.length;i++)
System.out.println("a["+i+"] = "+a[i]);
}}
运行结果:Exception in thread "RMI TCP Connection(1)-127.0.0.1" java.security.AccessControlException: access denied (java.net.SocketPermission 127.0.0.1:1737 accept,resolve)
at java.security.AccessControlContext.checkPermission(Unknown Source)
at java.security.AccessController.checkPermission(Unknown Source)
at java.lang.SecurityManager.checkPermission(Unknown Source)
at java.lang.SecurityManager.checkAccept(Unknown Source)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.checkAcceptPermission(Unknown Source)
at sun.rmi.transport.tcp.TCPTransport.checkAcceptPermission(Unknown Source)
at sun.rmi.transport.Transport$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at sun.rmi.transport.Transport.serviceCall(Unknown Source)
at sun.rmi.transport.tcp.TCPTransport.handleMessages(Unknown Source)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
那位大哥大姐知道这是为什么吗?为什么总是说访问被拒绝?我申请了一个固定的端口:1099,为什么用Arith obj = (Arith)Naming.lookup("//localhost/ArithServer");返回的端口确是1737呢?