2013-04-12 93 views
0

我想建立一個RMI服務器,我想這樣的Java 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

看起來你有一個像[這裏]類似的問題[1] [1]以後試試吧://stackoverflow.com/questions/1823305/rmi-connection-refused-with-localhost –

+0

@ Seid.M不,它沒有。該線程中的OP至少試圖啓動一個註冊表。這裏沒有證據。確切地說, – EJP

回答

0

您是否已啓動註冊表。 HTTP:打開註冊表
啓動rmiregistry的窗戶
rmiregistry的&爲Linux/Unix的

+0

。 @Downvoter請注意。 +1 – EJP