Topic: RMI问题,如何调用helloworld方法??

  Print this page

1.RMI问题,如何调用helloworld方法?? Copy to clipboard
Posted by: needd
Posted on: 2006-06-18 21:57

我完全按照这个文章说得去做,但是为何成功不了???

RMI是Java平台实现远程调用的规范,下面是一个小例子,本机测试通过

一共有三个java类,远程接口,服务端程序,客户端程序

远程接口:

import java.rmi.*;

public interface HelloIn extends java.rmi.Remote{
String sayHello() throws RemoteException;
}

服务端程序:

import java.rmi.*;
import java.net.*;
import java.rmi.registry.*;
import java.rmi.server.*;

public class Hello extends java.rmi.server.UnicastRemoteObject implements HelloIn{
public Hello() throws RemoteException{
super();
}
public String sayHello() throws RemoteException{
return "Hello,World!";
}
public static void main(String[] args){
//System.setSecurityManager(new java.rmi.RMISecurityManager());
try{

Hello h=new Hello();
java.rmi.Naming.rebind("hello",h);
System.out.print("Ready......");
}
catch(Exception e){
e.printStackTrace();
}

}
}

执行服务端程序前在命令行方式下启动rmi的注册程序: start rmiregistry

客户端程序:

import java.rmi.*;
import java.rmi.registry.*;

public class Helloworld{
public static void main(String[] args){
//System.setProperty( "java.security.policy", "client.policy" );
//System.setSecurityManager(new java.rmi.RMISecurityManager());
try{
HelloIn hi=(HelloIn)Naming.lookup("//fengl/hello");
for(int i=0;i<10;i++){
System.out.println(hi.sayHello());
}
}
catch(Exception e){
e.printStackTrace();
}
}
}

执行客户端程序前先用 rmic Hello 生成Stub 和 Skeleton 的class,它们
实际上是远程调用的底层的实现。

最后执行java Helloworld 控制台打印出 Hello,World,成功调用.

唯一的不太了解的是这句话,
HelloIn hi=(HelloIn)Naming.lookup("//fengl/hello");
它的参数应该是什么啊?
我把这三个类都放到了E:\test\下
lookup中应该些什么??
我写的是
HelloIn hi=(HelloIn)Naming.lookup("E:/test/hello");

2.Re:RMI问题,如何调用helloworld方法?? [Re: needd] Copy to clipboard
Posted by: needd
Posted on: 2006-06-18 23:21

java.rmi.UnknownHostException: Unknown host: test; nested exception is:
java.net.UnknownHostException: test
at sun.rmi.transport.tcp.TCPEndpoint.newSocket(Unknown Source)
at sun.rmi.transport.tcp.TCPChannel.createConnection(Unknown Source)
at sun.rmi.transport.tcp.TCPChannel.newConnection(Unknown Source)
at sun.rmi.server.UnicastRef.newCall(Unknown Source)
at sun.rmi.registry.RegistryImpl_Stub.lookup(Unknown Source)
at java.rmi.Naming.lookup(Unknown Source)
at Helloworld.main(Helloworld.javaShy
Caused by: java.net.UnknownHostException: test
at java.net.PlainSocketImpl.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.<init>(Unknown Source)
at java.net.Socket.<init>(Unknown Source)
at sun.rmi.transport.proxy.RMIDirectSocketFactory.createSocket(Unknown S
ource)
at sun.rmi.transport.proxy.RMIMasterSocketFactory.createSocket(Unknown S
ource)
... 7 more

这是错误提示

3.Re:RMI问题,如何调用helloworld方法?? [Re: needd] Copy to clipboard
Posted by: ancin
Posted on: 2006-06-19 09:20

应该在Server端实行注册的;
LocateRegistry.createRegistry(port);// Port是端口号
Naming.bind("//"+serverIp+":"+port+"/Service",h);
我想这样应该就可以了;我们都是这样做的。

4.Re:RMI问题,如何调用helloworld方法?? [Re: needd] Copy to clipboard
Posted by: needd
Posted on: 2006-06-19 11:54

楼上的朋友,您能说清楚点吗?
我的ip是127。0。0。1
/service是
E:\\test\\hello吗?
最好能给个全代码,谢了

5.Re:RMI问题,如何调用helloworld方法?? [Re: needd] Copy to clipboard
Posted by: needd
Posted on: 2006-06-19 14:30

搞好了,谢了朋友,


   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923