一個完整的Java RMI通訊的例子
阿新 • • 發佈:2019-02-02
完整的程式碼,請參考附件。
伺服器端:
客戶端:
伺服器端:
public class RmiUserServiceImpl extends UnicastRemoteObject implements
RmiUserService {
UserService service = new UserServiceImpl();
public RmiUserServiceImpl() throws RemoteException {
}
private static final long serialVersionUID = -7138363701504022490L;
public void create(String username, String password) throws RemoteException {
service.create(username, password);
}
public User login(String username, String password) throws RemoteException {
return service.login(username, password);
}
public static void main(String[] args) throws RemoteException, MalformedURLException, AlreadyBoundException {
LocateRegistry.createRegistry(1099);
Naming.bind("rmi://localhost:1099/UserService", new RmiUserServiceImpl());
System.out.print("Service start");
}
}
客戶端:
public class Client {
public static void main(String[] args) throws MalformedURLException, RemoteException, NotBoundException {
RmiUserService service = (RmiUserService) Naming
.lookup("rmi://localhost:1099/UserService");
//service.create("new_user_01", "new_user_01");
System.out.println(service.login("new_user_01", "new_password_01"));
}
}