Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 746
saplingPro Avatar asked Apr 14 '11 11:04

saplingPro


People also ask

How do I fix Java net ConnectException Connection refused connect in Java?

net. ConnectException: Connection refused: 1) First try to ping the destination host, if the host is ping-able it means the client and server machine are in the network. 2) Try connecting to server host and port using telnet.

How do I fix Java net SocketException connection timed out?

Using try/catch/finally If you are a developer, so you can surround the socket connection part of your code in a try/catch/finally and handle the error in the catch. You might try connecting a second time, or try connecting to another possible socket, or simply exit the program cleanly.

What does Java net ConnectException Connection refused mean?

Connection refused is a clear case of a client trying to connect on a TCP port but not able to succeed. Below are some of the possible reason why java.net.ConnectException: Connection refused comes: 1) Client and Server, either or both of them are not in the network. 2) Server is not running.


2 Answers

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

like image 178
Richard H Avatar answered Oct 24 '22 12:10

Richard H


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.

like image 24
Tater_Head Avatar answered Oct 24 '22 12:10

Tater_Head