я получаю отказ подключения ошибки при использовании Java RMIJava 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.
Правильно отформатируйте этот неразборчивый беспорядок. – EJP
Вы можете уточнить, как я буду форматировать его дальше? – HardLeeWorking