【问题标题】:Java Rmi ExceptionJava Rmi 异常
【发布时间】:2013-04-04 20:39:41
【问题描述】:

我想建一个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

请问我做错了什么?

提前致谢

【问题讨论】:

  • 看起来你有类似的问题,如 [this][1] [1]: stackoverflow.com/questions/1823305/…
  • @Seid.M 不,它没有。该线程中的 OP 至少正在尝试启动注册表。这里没有证据。

标签: java rmi java-server


【解决方案1】:

您是否启动了注册表。启动注册表后尝试
start rmiregistry for windows
rmiregistry & for Linux/Unix

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-10-13
    • 1970-01-01
    • 2016-02-17
    • 2016-05-29
    • 1970-01-01
    • 2010-10-11
    • 1970-01-01
    • 2012-12-24
    相关资源
    最近更新 更多