为什么我的 RMI 服务器在客户端可以连接之前就退出了?
Why does my RMI server exit before a client can connect?
在指定端口上 运行 启动程序后,程序在客户端能够连接到它之前终止。
try {
StudentService obj = new StudentService();
Registry r = LocateRegistry.createRegistry(4200);
r.bind("localhost", obj);
} catch (RemoteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (AlreadyBoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
这是 StudentService class 中已被覆盖的方法。
int multiply(int s, int b) throws RemoteException {
return s * b;
}
根据文档,
UnicastRemoteObject.exportObject exports the supplied remote object to receive incoming remote method invocations on an anonymous TCP port and returns the stub for the remote object to pass to clients
因此,您的程序在未侦听传入连接的情况下结束也就不足为奇了。这应该有效。
try {
StudentService obj = new StudentService();
UnicastRemoteObject.exportObject(obj, 0);
Registry r = LocateRegistry.createRegistry(4200);
r.bind("localhost", obj);
} catch (RemoteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (AlreadyBoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
在指定端口上 运行 启动程序后,程序在客户端能够连接到它之前终止。
try {
StudentService obj = new StudentService();
Registry r = LocateRegistry.createRegistry(4200);
r.bind("localhost", obj);
} catch (RemoteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (AlreadyBoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
这是 StudentService class 中已被覆盖的方法。
int multiply(int s, int b) throws RemoteException {
return s * b;
}
根据文档,
UnicastRemoteObject.exportObject exports the supplied remote object to receive incoming remote method invocations on an anonymous TCP port and returns the stub for the remote object to pass to clients
因此,您的程序在未侦听传入连接的情况下结束也就不足为奇了。这应该有效。
try {
StudentService obj = new StudentService();
UnicastRemoteObject.exportObject(obj, 0);
Registry r = LocateRegistry.createRegistry(4200);
r.bind("localhost", obj);
} catch (RemoteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (AlreadyBoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}