Error = java.rmi.ConnectException: Connection refused to host: 192.168.1.100; nested exception is: java.net.ConnectException: [closed]

StackOverflow https://stackoverflow.com/questions/21857094

  •  13-10-2022
  •  | 
  •  

Question

I am trying to implement a simple calcualtor using RMI in java. I ran the codes in NetBeans and using the command prompt. Both time I got some errors. When I ran it in NetBeans I got this error - "Error = java.rmi.ConnectException: Connection refused to host: 192.168.1.100; nested exception is: java.net.ConnectException: Connection refused: connect ". Why this error occurs ? I will provide my code below :

package rmi;
import java.rmi.*;

public interface CalcIntf extends Remote
{

double add(double d1,double d2)throws RemoteException;

double subtract(double d1,double d2)throws RemoteException;

double product(double d1,double d2)throws RemoteException;

double division(double d1,double d2)throws RemoteException;
}

package rmi;
import java.rmi.*;
import java.rmi.server.*;

public class CalcImpl extends UnicastRemoteObject implements CalcIntf
{

public CalcImpl()throws RemoteException
{
}

@Override
public double add(double d1,double d2)throws RemoteException
{
return(d1+d2);
}

@Override
public double subtract(double d1,double d2)throws RemoteException
{
return(d1-d2);
}

@Override
public double product(double d1,double d2)throws RemoteException
{
return(d1*d2);
}  

@Override
public double division(double d1,double d2)throws RemoteException
{
return(d1/d2);
}

}

package rmi;
import java.net.MalformedURLException;
import java.rmi.*;

public class ServerRMI
{

public static void main(String[] ar)
{
try
{
  CalcImpl cal=new CalcImpl();
  Naming.rebind("ServerRMI",cal);
}
catch(RemoteException | MalformedURLException e)
{
  System.out.println("Error = "+e);
}
}
}

package rmi;
import java.rmi.*;
import java.awt.*;
import java.awt.event.*;
import java.net.MalformedURLException;
import javax.swing.*;

public class ClientRMI extends JFrame implements ActionListener
{
TextField t1,t2;
Label l1,l2,l3;
Button b1,b2,b3,b4;

@SuppressWarnings("LeakingThisInConstructor")
public ClientRMI()
{
setLayout(null);
setSize(500,500);
setLocation(50,50);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setTitle("Calculator");
setVisible(true);

l1=new Label("Enter First Number :");
l1.setBounds(10,20,150,30);
add(l1);

t1=new TextField();
t1.setBounds(170,20,100,30);
add(t1);

l2=new Label("Enter Second Number :");
l2.setBounds(10,60,150,30);
add(l2);

t2=new TextField();
t2.setBounds(170,60,100,30);
add(t2);

b1=new Button("+");
b1.setBounds(20,100,30,30);
add(b1);
b1.addActionListener(this);

b2=new Button("-");
b2.setBounds(60,100,30,30);
add(b2);
b2.addActionListener(this);

b3=new Button("*");
b3.setBounds(100,100,30,30);
add(b3);
b3.addActionListener(this);

b4=new Button("/");
b4.setBounds(140,100,30,30);
add(b4);
b4.addActionListener(this);

l3=new Label();
l3.setBounds(100,150,150,30);
add(l3);
}

@Override
public void actionPerformed(ActionEvent ae)
{

double d1,d2;


if(ae.getSource()==b1)
{
try
{
  String url="rmi://127.0.0.1/ServerRMI";
CalcIntf calintf=(CalcIntf)Naming.lookup(url);
  d1=Double.valueOf(t1.getText()).doubleValue();
  d2=Double.valueOf(t2.getText()).doubleValue();
  l3.setText("Result = "+calintf.add(d1,d2));
  }
  catch(NotBoundException | MalformedURLException | RemoteException | 
              NumberFormatException e)
{
System.out.println("Error = " +e);
}
}

if(ae.getSource()==b2)
{
try
{

  String url="rmi://127.0.0.1/ServerRMI";
CalcIntf calintf=(CalcIntf)Naming.lookup(url);
  d1=Double.valueOf(t1.getText()).doubleValue();
  d2=Double.valueOf(t2.getText()).doubleValue();
  l3.setText("Result = "+calintf.subtract(d1,d2));
  }
  catch(NotBoundException | MalformedURLException | RemoteException | 
              NumberFormatException e)
{
System.out.println("Error = " +e);
}
}

if(ae.getSource()==b3)
{
try
{

String url="rmi://127.0.0.1/ServerRMI";
CalcIntf calintf=(CalcIntf)Naming.lookup(url);

  d1=Double.valueOf(t1.getText()).doubleValue();
  d2=Double.valueOf(t2.getText()).doubleValue();
  l3.setText("Result = "+calintf.product(d1,d2));
  }
  catch(NotBoundException | MalformedURLException | RemoteException | 
              NumberFormatException e)
{
System.out.println("Error = " +e);
}
}

if(ae.getSource()==b4)
{
try
{

 String url="rmi://127.0.0.1/ServerRMI";
CalcIntf calintf=(CalcIntf)Naming.lookup(url);
  d1=Double.valueOf(t1.getText()).doubleValue();
  d2=Double.valueOf(t2.getText()).doubleValue();
  l3.setText("Result = "+calintf.division(d1,d2));
  }
  catch(NotBoundException | MalformedURLException | RemoteException | 
              NumberFormatException e)
{
System.out.println("Error = " +e);
}
}
}

public static void main(String args[])
{
    ClientRMI cl=new ClientRMI();
}
}
Was it helpful?

Solution

Make sure that there's a server program running in the place where the client expects to find it (your message is not indicating a port, but you do have the IP address it's looking for). If you're certain that the server is running on that Address and port when the client starts, then the problem is probably that the system's firewall is blocking your access. How to deal with that is platform-specific.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top