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

男人不坏cc 男人不坏cc 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
服务器端

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端口会发现,会生成两个环路进程造成报错,不知道怎么回事,有的电脑就不会报错。求解。
...全文

相关推荐

评论 2
收藏 已收藏
分享
返回顶部
男人不坏cc
Java 51,407 成员 86,128 内容