一个简单的RMI例子,不明白怎么错

04-05-29 killme2008
刚接触RMI不久,按TJK上面的例子来的,不明白怎么错,汗
远程接口:PerfectTimer.java
package TJK.rmi;
import java.rmi.*;
interface PerfectTimer extends Remote
{
long getPerfectTime()throws RemoteException;
}

服务端实现远程对象:PerfectTime.java
package TJK.rmi;
import java.rmi.*;
import java.rmi.server.*;
import java.rmi.registry.*;
import java.net.*;

public class PerfectTime extends UnicastRemoteObject
implements PerfectTimer
{
public long getPerfectTime()throws RemoteException
{
return System.currentTimeMillis();
}
public PerfectTime()throws RemoteException{
}

public static void main(String args[])
throws Exception
{
System.setSecurityManager(new RMISecurityManager());
PerfectTime pt=new PerfectTime();
Naming.bind("PerfectTime",pt);
System.out.println("ready to do time");
}
}

客户端:DisplayPerfectTime.java
package TJK.rmi;
import java.rmi.*;
import java.rmi.registry.*;
public class DisplayPerfectTime
{
public static void main(String []args)
throws Exception
{
System.setSecurityManager(new RMISecurityManager());
PerfectTimer t=(PerfectTimer)Naming.lookup("rmi://localhost/PerfectTime");
for(int i=0;i<10;i++)
System.out.println("Perfect time="+t.getPerfectTime());
}
}

全部程序编译后,然后注册,
start rmiregistry
rmic TJK.rmi.PerfectTime
但是一运行服务端就出现异常
java.security.AccessControlException: access denied (java.net.SocketPermission 127.0.0.1:1099 connect,resolve)
at java.security.AccessControlContext.checkPermission(AccessControlContext.java:269)
at java.security.AccessController.checkPermission(AccessController.java:401)
at java.lang.SecurityManager.checkPermission(SecurityManager.java:524)
at java.lang.SecurityManager.checkConnect(SecurityManager.java:1026)
at java.net.Socket.connect(Socket.java:446)
at java.net.Socket.connect(Socket.java:402)
at java.net.Socket.<init>(Socket.java:309)
at java.net.Socket.<init>(Socket.java:124)
at sun.rmi.transport.proxy.RMIDirectSocketFactory.createSocket(RMIDirectSocketFactory.java:22)
at sun.rmi.transport.proxy.RMIMasterSocketFactory.createSocket(RMIMasterSocketFactory.java:128)
at sun.rmi.transport.tcp.TCPEndpoint.newSocket(TCPEndpoint.java:562)
at sun.rmi.transport.tcp.TCPChannel.createConnection(TCPChannel.java:185)
at sun.rmi.transport.tcp.TCPChannel.newConnection(TCPChannel.java:171)
at sun.rmi.server.UnicastRef.newCall(UnicastRef.java:313)
at sun.rmi.registry.RegistryImpl_Stub.lookup(Unknown Source)
at java.rmi.Naming.lookup(Naming.java:84)
at TJK.rmi.DisplayPerfectTime.main(DisplayPerfectTime.java:10)
Exception in thread "main"
应该是访问失败吧,该怎么改才行??谢谢
――――学生

banq
2004-06-01 18:41
没有设定安全策略文件。
RMI直接编程用处不太大了,学习理解EJB有用处。

IDFLY
2004-06-03 17:43
这个说法不好,要真正想学java rmi是必须要掌握的。

banq
2004-06-05 09:20
RMI本来是使用在肥客户端调用服务器,因为普通客户端和服务器之间存在防火墙,RMI无法穿透,所以,这一技术基于Http协议的技术代替,如Web Services,所以,RMI适合在一个防火墙内的调用,这种应用范围很小。

liusoft
2004-06-18 11:33
只是很小,并不能说没用 :) 在实现局域网内应用程序之间的通讯是多么的简单啊,EJB? WebService?都不适合,需要一大堆环境的支撑。