是否用RMI调用主机方法实现题 需要用jboss配置 而不能直接在JRE环境中连接主机,能解释一下原理吗?只在JDK环境下运行 会给出 JAVA IO EOF的错误或者 connect time out错误源代码
客户机和server端接口import java.rmi.Remote;
import java.rmi.RemoteException;
public interface Hello extends Remote {
String sayHello() throws RemoteException;
}
服务器端实现类
import java.rmi.registry.*;
import java.rmi.Naming;
import java.rmi.RemoteException;
import java.rmi.RMISecurityManager;
import java.rmi.server.UnicastRemoteObject;
public class HelloImpl extends UnicastRemoteObject
implements Hello {
public HelloImpl() throws RemoteException {
super();
}
public String sayHello () {
return "Hello World!";
} public static void main (String args []) {
// Create and install a security manager
if (System.getSecurityManager() == null) {
System.setSecurityManager(new RMISecurityManager());
} try {
HelloImpl obj = new HelloImpl();
Registry r = LocateRegistry.createRegistry(6566); r.bind("HelloServer", obj);
// Bind this object instance to the name "HelloServer"
System.out.println("HelloServer bound in registry");
} catch (Exception e) {
System.out.println("HelloImpl err: " + e.getMessage());
e.printStackTrace();
}
}
} 客户端调用类import java.rmi.* ;
import java.net.* ;
import java.io.*;import java.rmi.registry.*;
public class HelloApplet {
public static void main(String[] args)
{
Hello obj = null ;
try {
Registry r = LocateRegistry.getRegistry("113.55.35.206", 6566);
obj= (Hello) r.lookup("/HelloServer");
String message = obj.sayHello();
System.out.println("message"+message);
} catch (Exception e) {
System.out.println("HelloApplet exception: " +
e.getMessage());
e.printStackTrace();
}
} }
客户机和server端接口import java.rmi.Remote;
import java.rmi.RemoteException;
public interface Hello extends Remote {
String sayHello() throws RemoteException;
}
服务器端实现类
import java.rmi.registry.*;
import java.rmi.Naming;
import java.rmi.RemoteException;
import java.rmi.RMISecurityManager;
import java.rmi.server.UnicastRemoteObject;
public class HelloImpl extends UnicastRemoteObject
implements Hello {
public HelloImpl() throws RemoteException {
super();
}
public String sayHello () {
return "Hello World!";
} public static void main (String args []) {
// Create and install a security manager
if (System.getSecurityManager() == null) {
System.setSecurityManager(new RMISecurityManager());
} try {
HelloImpl obj = new HelloImpl();
Registry r = LocateRegistry.createRegistry(6566); r.bind("HelloServer", obj);
// Bind this object instance to the name "HelloServer"
System.out.println("HelloServer bound in registry");
} catch (Exception e) {
System.out.println("HelloImpl err: " + e.getMessage());
e.printStackTrace();
}
}
} 客户端调用类import java.rmi.* ;
import java.net.* ;
import java.io.*;import java.rmi.registry.*;
public class HelloApplet {
public static void main(String[] args)
{
Hello obj = null ;
try {
Registry r = LocateRegistry.getRegistry("113.55.35.206", 6566);
obj= (Hello) r.lookup("/HelloServer");
String message = obj.sayHello();
System.out.println("message"+message);
} catch (Exception e) {
System.out.println("HelloApplet exception: " +
e.getMessage());
e.printStackTrace();
}
} }
解决方案 »
- 如何动态从jsp中获取数据传向action,再展现出来
- Hibernate 问题
- myeclipse开发web service客户端
- styleReport
- struct2与xwork的关系
- Dom4J XPath 疑问
- 开源论坛JForum的研究,各位大虾请进!
- 讨论一下!请问大家都用什么工具开发struts
- 急寻JBulider7的注册码
- JSP页面权限控An exception occurred processing JSP page [/pages/main.jsp] at line [71]
- webservice服务端如何控制并发,请同志们指教!~
- 求一递归算法的方法 急急急!只有27分了
import java.rmi.*;
public interface HelloInterface extends Remote{
public String sayHello() throws RemoteException;
}Step 2:编写服务器类HelloImplementor,实现远程接口HelloInterface
import java.rmi.server.*;
import java.rmi.RemoteException;
public class HelloImplementor extends UnicastRemoteObject implements HelloInterface{
public HelloImplementor()throws RemoteException{
super();
}
public String sayHello() throws RemoteException{
return "from server:hello client";
}
}Step 3:生成stub和skeleton:
rmic –v1.2 testrmi. HelloImplementor
(这个步骤在我机器上不用也行)Step 4:编写一个主类HelloHelper,实例化服务器类HelloImplementor ,
生成远程对象,并且向注册库命名注册远程对象
import java.rmi.*;
import java.rmi.server.*;
import javax.naming.*;
public class HelloHelper{
public static void main(String[] args){
try{
HelloImplementor hi = new HelloImplementor();
Naming.bind("rmi://localhost:1099/hello",hi);
System.out.println("object bound");
}catch(Exception e){e.printStackTrace();}
}
}Step 5:编写客户机类,调用远程对象上的方法
import java.rmi.*;
public class HelloClient{
public static void main(String[] args){
try{
HelloInterface h = (HelloInterface)Naming.lookup("rmi://localhost:1099/hello");
System.out.println(h.sayHello());
}catch(Exception e){e.printStackTrace();}
}
}