java.rmi.ConnectException:拒绝连接主机:本地主机;在Eclipse中

时间:2015-04-16 19:48:55

标签: java eclipse exception connection rmi

好的,我目前正在使用Eclipse,当我运行程序时,我遇到了这些错误:

错误:

java.rmi.ConnectException:Connection拒绝主机:localhost;嵌套异常是:java.net.ConnectException:连接被拒绝:连接 任何人都知道为什么我会得到这些以及如何解决它们?

客户代码:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.util.List;


public class Client 
{
public static void main(String[] args) 
{


    Message_Server_Interface server = createServer();

    String key = null;
    String message = null;
    String input = null;
    int num = -1;

    do {
        PrintMenu();
        key = ReadKey();

        switch (key)
        {
        case "1":
            System.out.println("Enter your message");
            message = ReadKey();
            try 
            {
                server.addMessage(message);
            } catch (RemoteException e) 
            {
                e.printStackTrace();
            }
            break;
        case "2":
            try 
            {
                System.out.println(server.getMessage());
            } catch (RemoteException e) 
            {
                e.printStackTrace();
            }
            break;
        case "3":
            try 
            {
                System.out.println(server.getMessageCount());
            } catch (RemoteException e) 
            {
                e.printStackTrace();
            }
            break;
        case "4":
            try 
            {
                List<String> messages = server.getAllMessages();
                for(String str : messages)
                {
                    System.out.println(str);
                }
            } catch (RemoteException e) 
            {
                e.printStackTrace();
            }
            break;
        case "5":
            System.out.println("How many messages do you want to print?");
            input = ReadKey();

            try 
            {
                num = -1;
                num = Integer.parseInt(input);
            } catch(NumberFormatException nfe) 
            {
                System.err.println("Invalid Format!");
            }

            try {
                List<String> messages = server.getNMessages(num);
                for(String str : messages)
                {
                    System.out.println(str);
                }
            } catch (RemoteException e) 
            {
                e.printStackTrace();
            }
            break;
        case "6":
            System.out.println("How many recent messages do you want to       save?");
            input = ReadKey();

            try 
            {
                num = -1;
                num = Integer.parseInt(input);
            } catch(NumberFormatException nfe)
            {
                System.err.println("Invalid Format!");
            }

            try 
            {
                server.messagesToStore(num);
            } catch (RemoteException e) 
            {
                e.printStackTrace();
            }
            break;
        }

    } while (true);
}

private static Message_Server_Interface createServer() 
{
    String hostname = "localhost";
    Message_Server_Interface messageServer = null;

    try 
    {
        // locate the registry
        Registry registry = LocateRegistry.getRegistry(hostname);

        // then locate the message object stub
        messageServer = (Message_Server_Interface)     registry.lookup("Messages");

    } catch (Exception e) 
    {
        System.out.println("Error : " + e);
    }

    return messageServer;
}

private static void PrintMenu()
{
    System.out.println("Please select an option:");
    System.out.println("----------------------");
    System.out.println("1: Add a message");
    System.out.println("2: Print most recent message");
    System.out.println("3: Print number of messages");
    System.out.println("4: Print all messages");
    System.out.println("5: Return N messages");
    System.out.println("6: Retain only N messages");
}

private static String ReadKey()
{
    BufferedReader br = new BufferedReader(new      InputStreamReader(System.in));
    String key = null;
    try 
    {
        key = br.readLine();
    } catch (IOException e) 
    {
        e.printStackTrace();
    }

    return key;
}
}

服务器代码:

import java.rmi.server.UnicastRemoteObject;
import java.rmi.registry.Registry;
import java.rmi.registry.LocateRegistry;

public class Server_Setup 
{
public static void main(String args[])
{
    try 
    {
        Message_Server messageServer = new Message_Server();

        Message_Server_Interface messageStub = (Message_Server_Interface)     UnicastRemoteObject.exportObject(messageServer, 1200);

        Registry registry = LocateRegistry.createRegistry(1099);
        registry.rebind("Messages", messageStub);

    } catch (Exception e) 
    {
        System.out.println(e.getMessage());
    }

    System.out.println("Server started ...");
}
}

消息服务器代码:

import java.rmi.Remote;
import java.rmi.RemoteException;
import java.util.List;


interface Message_Server_Interface extends Remote
{
public void addMessage(String message) throws RemoteException;
public String getMessage() throws RemoteException;
public int getMessageCount() throws RemoteException;
public List<String> getAllMessages() throws RemoteException;
public List<String> getNMessages(int number) throws RemoteException;
public void messagesToStore(int number) throws RemoteException;
}

消息服务器代码:

import java.rmi.RemoteException;
import java.util.ArrayList;
import java.util.List;


public class Message_Server implements Message_Server_Interface
{
private ArrayList<String> messageArray;

// Default constructor
public Message_Server() 
{
    super();
    // initialise ArrayList to default size (10, according to javadocs)
    messageArray = new ArrayList<String>();
}

public void addMessage(String message) throws RemoteException 
{
    messageArray.add(message);
}

public String getMessage() throws RemoteException 
{

    return messageArray.get(messageArray.size() - 1);
}

public int getMessageCount() throws RemoteException 
{
    return messageArray.size();
}

public List<String> getAllMessages() throws RemoteException 
{
    return messageArray;
}

public List<String> getNMessages(int number) throws RemoteException 
{
    if (number > messageArray.size()) throw new IllegalArgumentException();

    List<String> recentMessages = new ArrayList<String>();

    for (int i = 1; i <= number; i++)
    {
        recentMessages.add(messageArray.get(messageArray.size() - i));
    }

    return recentMessages;
}

public void messagesToStore(int number) throws RemoteException 
{
    if (number > messageArray.size()) throw new IllegalArgumentException();

    List<String> retainedMessages = new ArrayList<String>();

    for (int i = 1; i <= number; i++)
    {
        retainedMessages.add(messageArray.get(messageArray.size() - i));
    }

    messageArray.clear();
    messageArray = (ArrayList<String>) retainedMessages;
}
}

1 个答案:

答案 0 :(得分:0)

我在另一个programm上遇到了相同的问题,并通过一些步骤解决了该问题:

  1. 编译的类

    enter image description here


  1. 使用以下命令运行RMI注册表: start rmiregistry。 (如果我不这样做,那么我会收到此错误。)

不使用此命令,让我们看看:

enter image description here


运行服务器:

enter image description here


现在,我将使用此命令start rmiregistry

enter image description here


重新运行服务器,再没有出现问题:

enter image description here

我希望它可以帮助某人:)