java.rmi.ConnectException: Connection refused to host: 127.0.0.1

I've tried to used RMI, here is server side. at first it worked without any exception, but now after three times whenever i try to run the below code, i will get some errors

The code is:

import java.rmi.server.UnicastRemoteObject;

/**
 * Created by elyas on 12/11/14 AD.
 */
public class LogicImplement extends UnicastRemoteObject implements Logic
{

    public LogicImplement() throws Exception
    {
        java.rmi.registry.LocateRegistry.createRegistry(6060);
        java.rmi.Naming.rebind("Object1",this);
    }

    @Override
    public int sum(int a, int b) throws Exception
    {
        int result = a + b;
        System.out.println("ana sum executed");
        return result;
    }

    public static void main(String[] args)
    {
        try
        {
            LogicImplement logicImplement  = new LogicImplement();
        } catch (Exception e)
        {
            e.printStackTrace();
        }
    } 
} 

The error is like this: i've tried to change the Object1 to for example Object2, but again i will get error, also i change the port number...

what is solution?

java.rmi.ConnectException: Connection refused to host: 127.0.0.1; nested exception is: 
    java.net.ConnectException: Connection refused
    at sun.rmi.transport.tcp.TCPEndpoint.newSocket(TCPEndpoint.java:619)
    at sun.rmi.transport.tcp.TCPChannel.createConnection(TCPChannel.java:216)
    at sun.rmi.transport.tcp.TCPChannel.newConnection(TCPChannel.java:202)
    at sun.rmi.server.UnicastRef.newCall(UnicastRef.java:341)
    at sun.rmi.registry.RegistryImpl_Stub.rebind(Unknown Source)
    at java.rmi.Naming.rebind(Naming.java:177)
    at LogicImplement.<init>(LogicImplement.java:12)
    at LogicImplement.main(LogicImplement.java:27)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)
Caused by: java.net.ConnectException: Connection refused
    at java.net.PlainSocketImpl.socketConnect(Native Method)
    at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:339)
    at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:200)
    at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:182)
    at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
    at java.net.Socket.connect(Socket.java:579)
    at java.net.Socket.connect(Socket.java:528)
    at java.net.Socket.<init>(Socket.java:425)
    at java.net.Socket.<init>(Socket.java:208)
    at sun.rmi.transport.proxy.RMIDirectSocketFactory.createSocket(RMIDirectSocketFactory.java:40)
    at sun.rmi.transport.proxy.RMIMasterSocketFactory.createSocket(RMIMasterSocketFactory.java:147)
    at sun.rmi.transport.tcp.TCPEndpoint.newSocket(TCPEndpoint.java:613)
    ... 12 more

Upvotes: 4

Views: 41297

Answers (4)

Dula
Dula

Reputation: 1436

Since this was a port number issue, there is another way to start the registry on the port number you want:

java.rmi.registry.Registry rmiRegistry = java.rmi.registry.LocateRegistry.
                createRegistry(6060); // Creates a registry on 6060
rmiRegistry.rebind("Object1",this);   // Binds to registry created on 6060

Upvotes: 1

The Answer was very simple, By default, the registry runs on port 1099. To start the registry on a different port, specify the port number on the command line. Do not forget to unset your CLASSPATH environment variable. for more information check this link: Running the Example Programs

** So for fixing this code i must change the port number form 6060 to 1099

notice that: if 1099 is used by other services you have to test 1100, and if 1100 is used too, you have yo use 1101 and so on. :-)

Upvotes: 2

user207421
user207421

Reputation: 310903

You haven't started the RMI Registry.

When you get past this, if it still happens when calling the remote method, see item A.1 of the RMI FAQ.

Upvotes: 0

Darshan Lila
Darshan Lila

Reputation: 5868

java.net.ConnectException: Connection refused

There could be couple of reasons for this exception:

  • You have not started your rmiregistry in background.
  • You are trying to connect to the wrong port number.
  • Your firewall maybe blocking the connections.

Upvotes: 2

Related Questions