2013-04-12 67 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