java.net.ConnectException :connection timed out: connect?
Asked Answered
Z

4

13

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

Zorina answered 14/4, 2011 at 11:7 Comment(3)
Post the stack trace of the exception if anyLithology
Simplest explanation: your server's IP address is not what you think it isShad
The usual reason for this is solved by item A.1 in the RMI FAQ.Zitella
A
49

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

Ananna answered 14/4, 2011 at 12:14 Comment(6)
@ Richard of these i dont see any reason.Zorina
@Meprog - even though you don't see any reason, they're things that you should investigate and test, because they're the most likely reasons for a connection to time out. You should also take other peoples' advice and edit your post with the complete stack trace.Shad
Neither (a) not (b) is correct. Both would produce 'connection refused'.Zitella
For java, use host = "localhost"Pericarditis
@shihab_returns Use it how? Where? Why? What are you talking about?Zitella
g) You live in a country that the US government has sanctioned.Seriatim
A
1

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.

Anamariaanamnesis answered 3/2, 2012 at 20:2 Comment(2)
took me days of searching to find this, which answered a problem I was having with ehcache multicasting...isp had the wrong IP setup in the /etc/hosts file for the machine. Great insight!Logomachy
In my case, the problem was that I had the same IP of the hots I was trying to connect to, in my IPv4. The network had automatically assigned me exactly the same IP.Witte
S
0

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
Squarrose answered 2/4, 2018 at 13:52 Comment(0)
F
-2

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
Farming answered 9/10, 2017 at 23:57 Comment(1)
Why? Port 389 is LDAP. This is Java RMI.Zitella

© 2022 - 2024 — McMap. All rights reserved.