[java] java.net.ConnectException :connection timed out: connect?

I have used RMI in my code :

 import java.rmi.*;

 public interface AddServerIntf extends Remote {
  double add(double d1,double d2) throws RemoteException;
 }

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

public class AddServerImpl extends UnicastRemoteObject implements AddServerIntf {
  public AddServerImpl() throws RemoteException {
  }

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

import java.net.*;
import java.rmi.*;

public class AddServer {
   public static void main(String args[]) {
    try {
     AddServerImpl addServerImpl=new AddServerImpl();
     Naming.rebind("AddServer",addServerImpl);
    }  catch(Exception exc) {
          System.out.println(exc);
       }
   }
}

import java.rmi.*;
public class AddClient {
  public static void main(String args[]) {
     try {
       String Url="rmi://"+args[0]+"/AddServer";
       AddServerIntf addServerIntf=(AddServerIntf)Naming.lookup(Url);
       System.out.println("The first number is "+args[1]);
       double d1=Double.valueOf(args[1]).doubleValue();
       System.out.println("The second number is: "+args[2]);
       double d2=Double.valueOf(args[2]).doubleValue();
       System.out.println("The Sum is: "+addServerIntf.add(d1,d2));
     }  catch(Exception exc) {
         System.out.println(exc);
       }
   }
 }

These are 4 .java files written.

Next i compile all these files.Then I create a stub using rmic AddServerImpl. After that i start rmi registry on server side using start rmiregistry. Then i start server using java AddServer and finally client using java AddClient 27.60.200.80 5 9. But nothing happens

Exception that is thrown on client side is java.net.ConnectException : connection timed out : connect

What is the reason and how can i solve this?

On client machine these are the following .class files AddClient.class AddServerImpl.class AddServerImpl_Stub.class and on server side AddServer.class AddServerImpl.class AddServerImpl_Stub.class AddServerIntf.class

This question is related to java networking rmi stub

The answer is


The error message says it all: your connection timed out. This means your request did not get a response within some (default) timeframe. The reasons that no response was received is likely to be one of:

  • a) The IP/domain or port is incorrect
  • b) The IP/domain or port (i.e service) is down
  • c) The IP/domain is taking longer than your default timeout to respond
  • d) You have a firewall that is blocking requests or responses on whatever port you are using
  • e) You have a firewall that is blocking requests to that particular host
  • f) Your internet access is down

Note that firewalls and port or IP blocking may be in place by your ISP


If you're pointing the config at a domain (eg fabrikam.com), do an NSLOOKUP to ensure all the responding IPs are valid, and can be connected to on port 389:

NSLOOKUP fabrikam.com

Test-NetConnection <IP returned from NSLOOKUP> -port 389

Number (1): The IP was incorrect - is the correct answer. The /etc/hosts file (a.k.a. C:\Windows\system32\drivers\etc\hosts ) had an incorrect entry for the local machine name. Corrected the 'hosts' file and Camel runs very well. Thanks for the pointer.


Exception : java.net.ConnectException

This means your request didn't getting response from server in stipulated time. And their are some reasons for this exception:

  • Too many requests overloading the server
  • Request packet loss because of wrong network configuration or line overload
  • Sometimes firewall consume request packet before sever getting
  • Also depends on thread connection pool configuration and current status of connection pool
  • Response packet lost during transition

Examples related to java

Under what circumstances can I call findViewById with an Options Menu / Action Bar item? How much should a function trust another function How to implement a simple scenario the OO way Two constructors How do I get some variable from another class in Java? this in equals method How to split a string in two and store it in a field How to do perspective fixing? String index out of range: 4 My eclipse won't open, i download the bundle pack it keeps saying error log

Examples related to networking

Access HTTP response as string in Go Communication between multiple docker-compose projects Can't access 127.0.0.1 How do I delete virtual interface in Linux? ConnectivityManager getNetworkInfo(int) deprecated Bridged networking not working in Virtualbox under Windows 10 Difference between PACKETS and FRAMES How to communicate between Docker containers via "hostname" java.net.ConnectException: failed to connect to /192.168.253.3 (port 2468): connect failed: ECONNREFUSED (Connection refused) wget: unable to resolve host address `http'

Examples related to rmi

java.rmi.ConnectException: Connection refused to host: 127.0.1.1; java.net.ConnectException :connection timed out: connect? What port is used by Java RMI connection? What is the difference between Java RMI and RPC? Rmi connection refused with localhost

Examples related to stub

What does "to stub" mean in programming? java.net.ConnectException :connection timed out: connect? What's the difference between a mock & stub? Access restriction on class due to restriction on required library rt.jar?