【问题标题】:Java RMI connection failure [duplicate]Java RMI连接失败[重复]
【发布时间】:2016-03-08 18:51:45
【问题描述】:

我在使用 JAVA RMI 时不断收到连接失败错误

我的代码: //mathServer//

import java.rmi.*;
import java.rmi.Naming.*;
import java.rmi.server.*;
import java.rmi.registry.*;
import java.net.*;
import java.util.*;



interface mathInterface extends Remote
{
 public int add(int a,int b) throws RemoteException;
 public int subt(int a,int b) throws RemoteException;
 public int mult(int a,int b) throws RemoteException;
 public int div(int a,int b) throws RemoteException;
}

public class mathServer extends UnicastRemoteObject implements
mathInterface

{
            /**
 * 
 */
private static final long serialVersionUID = 1L;
            public mathServer() throws RemoteException
            {
                            System.out.println("Initializing Server");


}
public int add(int a,int b)
{
                            return(a+b);
            }
            public int subt(int a,int b)
                {
                                            return(a-b);
            }
            public int mult(int a,int b)
                {
                                            return(a*b);
            }
            public int div(int a,int b)
                {
                                            return(a/b);
            }
            public static void main(String args[])
            {
                            try
                            {
                            mathServer ms=new mathServer();
                            java.rmi.Naming.rebind("MathServ",ms);
                            System.out.println("Server Ready");
                }
                catch(RemoteException RE)
                {
                                            System.out.println("Remote Server Error:"+ RE.getMessage());
                                            System.exit(0);
                            }
                            catch(MalformedURLException ME)
                            {
                                            System.out.println("Invalid URL!!");
                            }
            }

}

当我运行它时,我收到以下错误:

Remote Server Error:Connection refused to host: 192.168.56.1; nested exception is: 
java.net.ConnectException: Connection refused: connect

客户端的其余代码是:

//mathClient//
import java.rmi.*;  //this packaged performs OO equivalent of remote         procedure calls -  asterisks used to import the whole package rather than a     specific class
import java.rmi.registry.*; // bootstrap naming service that is used by RMI servers on the same host to bind remote objects to names
import java.awt.*; //gui toolkit
import java.awt.event.*; //events for the GUI toolkit

public class 


mathClient extends Frame implements ActionListener //Frame is the GUI toolkit basic class
{
            Button B1=new Button("Sum"); //creates new button on the calculator
            Button B2=new Button("Subtract");
            Button B3=new Button("Multiply");
            Button B4=new Button("Divide");
            Button B5=new Button("Enter");//creates new button on the calculator needs to have bounds set before active
            Label l1=new Label("Number 1"); //creates a label for the text entry fields
            Label l2=new Label("Number 2");
            Label l3=new Label("Result");
            Label l4=new Label("TextEntry");
            TextField t1=new TextField(20);  //creates text entry fields needs to have bounds set before active
            TextField t2=new TextField(20);
            TextField t3=new TextField(20);
            TextField t4=new TextField(20);
            public mathClient()
            {
                            super("Calculator");
                            setLayout(null);
                            l1.setBounds(20,50,55,25);
                            add(l1);
                            l2.setBounds(20,100,55,25);
                            add(l2);
                            l3.setBounds(20,150,55,25);
                            add(l3);
                            t1.setBounds(150,50,100,25);
                            add(t1);
                            t2.setBounds(150,100,100,25);
                            add(t2);
                            t3.setBounds(150,150,100,25);
                            add(t3);
                            t4.setBounds(150,300,100,50); //sets the size of the text box as per button size above
                            add(t4); //add the text box, can comment out to hide
                            B1.setBounds(20,200,80,25); //set size of the buttons, again need to identify dimensions
                            add(B1);
                            B2.setBounds(100,200,80,25);
                            add(B2);
                            B3.setBounds(180,200,80,25);
                            add(B3);
                            B4.setBounds(260,200,80,25);
                            add(B4);
                            B5.setBounds(320,200,80,25);  //this sets the location of the button on the screen, the first number is horizontal position, the second is the vertical position, third is the width of the button and 4th is the height of the button
                            add(B5); //this is used to show the button, comment it out to hide it from view
                            B1.addActionListener(this); //creates a listener for the button push
                            B2.addActionListener(this);
                            B3.addActionListener(this);
                            B4.addActionListener(this);
                            B5.addActionListener(this);
                            addWindowListener(
                                            new WindowAdapter()
                                                            {
                                                                            public void windowClosing(WindowEvent e)//used for closing the calculator window
                                                                            {
                                                                                            System.exit(0);
                                                                            }
                                                            }
                            );
            }
            public void actionPerformed(ActionEvent AE)//checks when an action occurs in the wid=ndow
            {
                                            if(AE.getSource()==B1)//checks which button has had an acionable event
                                            {
                                                            sum();//calls the relevant method, in this case it sums the inputs
                                            }
                                            else if(AE.getSource()==B2)
                                            {
                                                            subt();
                                            }
                                            else if(AE.getSource()==B3)
                                            {
                                                            mult();
                                            }
                                            else if(AE.getSource()==B4)
                                            {
                                                             div();
                                            }
                            }
                            public void sum() // the method for the sum button
                            {
                                            int i=Integer.parseInt(t1.getText()); //used for converting the entry from text fields from text to int
                                            int j=Integer.parseInt(t2.getText()); // used for converting from the 2nd text field
                                            int val; //used for result i expect
                                            try
                                            {
                                                            String ServerURL="MathServ";
                                                            mathInterface MI=(mathInterface)Naming.lookup(ServerURL);
                                                            val=MI.add(i,j);
                                                            t3.setText(""+val);
                                            }
                                            catch(Exception ex)
                                            {
                                                            System.out.println("Exception:"+ex);
                                            }
                            }
                            public void subt()
                            {
                                            int i=Integer.parseInt(t1.getText());
                                            int j=Integer.parseInt(t2.getText());
                                            int val;
                                            try
                                            {
                                                            String ServerURL="MathServ";
                                                            mathInterface MI=(mathInterface)Naming.lookup(ServerURL);
                                                            val=MI.subt(i,j);
                                                            t3.setText(""+val);
                                            }
                                            catch(Exception ex)
                                            {
                                                            System.out.println("Exception:"+ex);
                                            }
                            }
                            public void mult()
                            {
                                            int i=Integer.parseInt(t1.getText());
                                            int j=Integer.parseInt(t2.getText());
                                            int val;
                                            try
                                            {
                                                            String ServerURL="MathServ";
                                                            mathInterface MI=(mathInterface)Naming.lookup(ServerURL);
                                                            val=MI.mult(i,j);
                                                            t3.setText(""+val);
                                            }
                                            catch(Exception ex)
                                            {
                                                            System.out.println("Exception:"+ex);
                                            }
                            }
                            public void div()
                            {
                                            int i=Integer.parseInt(t1.getText());
                                            int j=Integer.parseInt(t2.getText());
                                            int val;
                                            try
                                            {
                                                            String ServerURL="MathServ";
                                                            mathInterface MI=(mathInterface)Naming.lookup(ServerURL);
                                                            val=MI.div(i,j);
                                                            t3.setText(""+val);
                                            }
                                            catch(Exception ex)
                                            {
                                                            System.out.println("Exception:"+ex);
                                            }
                            }
                            public static void main(String args[])
                            {
                                            mathClient MC=new mathClient();
                                            MC.setVisible(true);
                                            MC.setSize(600,500);
                            };
            } 

我尝试设置 etc`hosts 文件,但那里似乎没有问题。我不知道为什么它试图连接到 IP 192.168.56.1 而不是 127.0.0.1

任何帮助将不胜感激,这是我第一次在服务器端做任何事情,所以我错过了一些明显的东西,但我已经尝试了 RMI 无法连接的其他解决方案。

【问题讨论】:

  • 请正确格式化这个难以辨认的混乱。
  • 你能解释一下我将如何进一步格式化它吗?

标签: java rmi


【解决方案1】:

您尚未启动 RMI 注册表。

【讨论】:

  • 谢谢,我已经这样做了,但它似乎没有做任何事情,今天早上重试,现在我得到了不同的错误,但至少这是进步。
  • 所以你以为你已经做到了,但你没有。
  • 我从命令行运行了 javaw rmiregistry 但它什么也没做,当我从另一台机器上尝试时它启动了 rmiregistry 并导致了另一个错误。所以我已经做到了,但我认为问题与我笔记本电脑上的端口未打开有关。
  • 如果您收到此错误,您没有启动注册表,期间。注意:RMI 注册表必须在调用bind() 的同一主机上运行。
猜你喜欢
  • 1970-01-01
  • 2014-11-15
  • 1970-01-01
  • 2011-07-22
  • 1970-01-01
  • 1970-01-01
  • 2021-07-30
  • 1970-01-01
  • 2021-06-23
相关资源
最近更新 更多