java rmi执行的时候遇到的问题,大家帮帮忙??

男人不坏cc 2015-08-26 04:12:10
服务器端

package org.ngweb.rmi;

import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
/**
* The implementation class of the remote method defined in interface
*
* @author Huangng
*
*/
public class Rmi extends UnicastRemoteObject implements RmiInterface {
private static final long serialVersionUID = 1L;

// The constructor have to throw RemoteException
public Rmi() throws RemoteException {
}

// The implemented remote method.
public String say(String name) throws RemoteException {
System.out.println("Called by HelloClient");
return "Hello, " + name;
}
}
package org.ngweb.rmi;

import java.rmi.Remote;
import java.rmi.RemoteException;

/**
*
* An interface that define the remote method, which should be in both server
* and client end.
*
* @author Huangng
*
*/
public interface RmiInterface extends Remote {
// Any remote method have to throw RemoteException
public String say(String name) throws RemoteException;
}
package org.ngweb.rmi;

import java.rmi.Naming;
import java.rmi.registry.LocateRegistry;

/**
*
* Java RMI server
*
* @author Huangng
*
*/
public class RmiServer {

public static void main(String[] argv) {
try {
// set port number to 1099
LocateRegistry.createRegistry(1099);

// The instance of class with remote method
RmiInterface hello = new Rmi();

// Register the instance
Naming.rebind("Hello", hello);

System.out.println("Starting RMI Server success.");
} catch (Exception e) {
System.out.println("Starting RMI Server failed: " + e);
}
}
}

//客户端

package org.ngweb.rmi;

import java.rmi.Naming;

/**
*
* A client end of Java RMI
*
* @author Huangng
*
*/
public class RmiClient {

public static void main(String[] argv) {
try {
// Find an instance of Hello in remote machine
RmiInterface hello = (RmiInterface) Naming
.lookup("//127.0.0.1:1099/Hello");

// Call remote method
System.out.println(hello.say("huang"));
} catch (Exception e) {
System.out.println("RMI Client exception: " + e);
}
}
}
package org.ngweb.rmi;

import java.rmi.Remote;
import java.rmi.RemoteException;

/**
*
* An interface that define the remote method, which should be in both server
* and client end.
*
* @author Huangng
*
*/
public interface RmiInterface extends Remote {
// Any remote method have to throw RemoteException
public String say(String name) throws RemoteException;
}


服务器启动后,第二次打开客户端会报错;监听1099端口会发现,会生成两个环路进程造成报错,不知道怎么回事,有的电脑就不会报错。求解。
...全文
147 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
男人不坏cc 2015-08-26
  • 打赏
  • 举报
回复
请大家帮忙看看,我是新手,没写过这方面的代码
男人不坏cc 2015-08-26
  • 打赏
  • 举报
回复

50,456

社区成员

发帖
与我相关
我的任务
社区描述
Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧