Java RMI连接失败

Java RMI连接失败,java,rmi,Java,Rmi,在使用JavaRMI时,我一直遇到连接失败错误 我的代码: //数学服务器// 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)

在使用JavaRMI时,我一直遇到连接失败错误

我的代码: //数学服务器//

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无法连接的问题。

您还没有启动RMI注册表。

请正确格式化这个难以辨认的混乱。您能澄清我将如何进一步格式化它吗?谢谢,我这样做了,但它似乎没有做任何事情,今天早上重试,现在我得到了不同的错误,但至少这是进步。所以你认为你做了,但你没有。我曾从命令行运行javaw rmiregistry,但它什么也没做,当我从另一台机器尝试时,它启动了rmiregistry,并导致了不同的错误。所以我已经这样做了,但我认为问题与我笔记本电脑上的端口未打开有关。如果你出现此错误,你没有启动注册表,句号。注意:RMI注册表必须在调用
bind()
的同一主机上运行。