我想构建一个RMI服务器,我试过这个
package first_project;
import java.rmi.Naming;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
import java.util.ArrayList;
public class Server extends UnicastRemoteObject implements ServerInterface {
// list for know users
protected ArrayList<ClientInterface> clients = new ArrayList<ClientInterface>();
public Server() throws RemoteException {}
//logged in clients get a notification that a new user has joined the chat
// remote reference to the new client is added to the ArrayList.
public void login(ClientInterface client, String nickname) throws RemoteException {
broadcastMessage("--> " + nickname + " is entering the chatroom", "");
clients.add(client);
}
// used for broadcasting an incoming message
// and the nickname of its sender to all currently logged in clients .
//remote call of the method getMessage which is
public void broadcastMessage(String message, String nickname) throws RemoteException {
for (int i = 0; i < clients.size(); i++) {
ClientInterface c = clients.get(i);
try {
c.getMessage(message, nickname);
} catch (RemoteException e) {
logout(c);
i = i - 1;
} }}
//remove user
public void logout(ClientInterface client) {
clients.remove(client);
}
public static void main(String[] args) {
try {
Naming.rebind("Server", new Server());
System.out.println("Server is ready");
} catch (Exception e) {
e.printStackTrace();
}
}
}
但我不知道为什么我会遇到这个例外:
java.rmi.ConnectException: Connection refused to host: 192.168.1.3; nested exception is:
java.net.ConnectException: Connection refused: connect
at sun.rmi.transport.tcp.TCPEndpoint.newSocket(TCPEndpoint.java:601)
at sun.rmi.transport.tcp.TCPChannel.createConnection(TCPChannel.java:198)
at sun.rmi.transport.tcp.TCPChannel.newConnection(TCPChannel.java:184)
at sun.rmi.server.UnicastRef.newCall(UnicastRef.java:322)
at sun.rmi.registry.RegistryImpl_Stub.rebind(Unknown Source)
at java.rmi.Naming.rebind(Naming.java:160)
at first_project.Server.main(Server.java:47)
Caused by: java.net.ConnectException: Connection refused: connect
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:333)
at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:195)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:182)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:366)
at java.net.Socket.connect(Socket.java:519)
at java.net.Socket.connect(Socket.java:469)
at java.net.Socket.<init>(Socket.java:366)
at java.net.Socket.<init>(Socket.java:180)
at sun.rmi.transport.proxy.RMIDirectSocketFactory.createSocket(RMIDirectSocketFactory.java:22)
at sun.rmi.transport.proxy.RMIMasterSocketFactory.createSocket(RMIMasterSocketFactory.java:128)
at sun.rmi.transport.tcp.TCPEndpoint.newSocket(TCPEndpoint.java:595)
... 6 more
请问我做错了什么?
提前致谢
答案 0 :(得分:0)
您是否已启动注册表。启动注册表后尝试使用它
启动rmiregistry for windows
适用于Linux / Unix的 rmiregistry&amp;