無法修復此問題3天。 RMI可以在1臺或2臺PC上與localhost或192.168。*正常工作,但遠程客戶端不能使用遠程方法。 啓動rmiregisry,然後綁定服務與Naming.rebind()無法使用RMI遠程方法
註冊表正在創建成功,我可以得到綁定服務的列表,但我甚至不能使用任何方法。
搜索遠程服務與
Naming.lookup("rmi://host_ip:1099/qwe).
or
Registry reg = LocateRegistry.getRegistry("host_ip",server);
reg.lookup("qwe")
客戶端找到對象,但不能使用它了方法。
下面是代碼: Server.java:
public class Server extends UnicastRemoteObject{
protected Server() throws RemoteException {
}
public static void main(String[] args) throws InterruptedException, RemoteException {
ServiceServerImpl server = new ServiceServerImpl();
try {
System.setProperty("java.rmi.server.hostname", "host_ip");
Naming.rebind("/qwe", server);
Arrays.asList(Naming.list("rmi://localhost")).forEach(System.out::println);
System.out.println(System.getProperty("java.rmi.server.hostname"));
} catch (Exception e) {
e.printStackTrace();
}
server.startServer();
}
}
ServiceServer.java:
public interface ServiceServer extends Remote{
String[] getLoginsList() throws RemoteException;
void addLogin(String login) throws RemoteException;
void removeLogin(String login) throws RemoteException;
boolean canConnect(String login) throws RemoteException;
void setConnected(String login) throws RemoteException;
void setDisconnected(String login) throws RemoteException;
}
ServiceServerImpl.java(如果需要):
public class ServiceServerImpl extends UnicastRemoteObject implements ServiceServer {
private ServerSocket socketListener;
private Socket clientSock;
private volatile List<ClientHandler> threadsList = new ArrayList<ClientHandler>();
private volatile Map<String, Boolean> logins = new HashMap<String,Boolean>();
// private Timer timer;
public ServiceServerImpl() throws RemoteException {
}
public void startServer() {
// startTimer();
try {
socketListener = new ServerSocket(5001);
System.out.println("server up...");
while (true) {
clientSock = null;
while (clientSock == null)
clientSock = socketListener.accept();
ClientHandler curClientHandler = new ClientHandler(clientSock, this);
threadsList.add(curClientHandler);
System.out.println("user connected: " + clientSock.getInetAddress());
}
} catch (Exception e) {
System.err.println("Socket exception");
e.printStackTrace();
}
}
public synchronized void removeUserThread(ClientHandler handler) {
try {
threadsList.remove(handler);
} catch (NullPointerException e) {
}
System.out.println("- 1");
}
@Override
public void setConnected(String login){
logins.put(login,true);
}
@Override
public void setDisconnected(String login){
logins.put(login,false);
}
@Override
public boolean canConnect(String login) throws RemoteException{
return logins.keySet().contains(login)&& (!logins.get(login));
}
@Override
public String[] getLoginsList() throws RemoteException {
return this.logins.keySet().toArray(new String[this.logins.size()]);
}
@Override
public void addLogin(String login) throws RemoteException {
logins.put(login, false);
System.out.println("added");
}
@Override
public void removeLogin(String login) throws RemoteException {
logins.remove(login);
}
public Map<String, Boolean> getLogins() {
return logins;
}
public Set<String> getAllowedLogins() {
return logins.keySet();
}
public List<ClientHandler> getThreadsList() {
return threadsList;
}
}
即時通訊開始用cmd或eclipse進行rmiregistry,然後啓動Sever。 它正確地開始,我不能與本地主機或192.168.0連接* 但IT遠程客戶端不工作 儘量選用.policy文件 - 仍然不起作用
我得到下一個錯誤代碼:
Exception in thread "main" java.rmi.ConnectException: Connection refused to host: 192.168.0.83; nested exception is:
java.net.ConnectException: Connection timed out: connect
at sun.rmi.transport.tcp.TCPEndpoint.newSocket(Unknown Source)
at sun.rmi.transport.tcp.TCPChannel.createConnection(Unknown Source)
at sun.rmi.transport.tcp.TCPChannel.newConnection(Unknown Source)
at sun.rmi.server.UnicastRef.invoke(Unknown Source)
at java.rmi.server.RemoteObjectInvocationHandler.invokeRemoteMethod(Unknown Source)
at java.rmi.server.RemoteObjectInvocationHandler.invoke(Unknown Source)
at com.sun.proxy.$Proxy0.canConnect(Unknown Source)
at server.Main.main(Main.java:16)
Caused by: java.net.ConnectException: Connection timed out: connect
at java.net.DualStackPlainSocketImpl.connect0(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
at java.net.PlainSocketImpl.connect(Unknown Source)
at java.net.SocksSocketImpl.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.<init>(Unknown Source)
at java.net.Socket.<init>(Unknown Source)
at sun.rmi.transport.proxy.RMIDirectSocketFactory.createSocket(Unknown Source)
at sun.rmi.transport.proxy.RMIMasterSocketFactory.createSocket(Unknown Source)
... 8 more
請幫助
可能存在連接問題,如防火牆或其他設置。 嘗試通過telnet從客戶端連接到服務器:telnet server_ip server_port –
'Server'不應該擴展UnicastRemoteObject。 'ServiceServerImpl'不應該創建套接字。 – EJP
@EJP固定..仍然不工作 – shaldnikita