vcmkrtchyan
vcmkrtchyan

Reputation: 2626

How to set a custom port for RMI regisrty?

In my RMI program I want to set up my RMI registry on the port 8080, but when I do it, I get an exception.

Here is my server code

public class Server {
    public static void main(String[] args) {
        try {
            LocateRegistry.createRegistry(8080);
            MathServerImpl mathServer = new MathServerImpl();
            Naming.rebind("MathServer", mathServer);
            System.out.println("Math Server has started and is running");
        } catch (RemoteException | MalformedURLException e) {
            System.out.println(e.getMessage());
            e.printStackTrace();
        }
    }
}

The code works properly for the port 1099, which is it's default port as far as I know, but for this case it gives me a java.net.ConnectException, and here is the log.

java.rmi.ConnectException: Connection refused to host: 10.100.25.173; 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 server.Server.main(Server.java:16)
    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:134)
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)
    ... 11 more

Upvotes: 0

Views: 2359

Answers (3)

Ian Roberts
Ian Roberts

Reputation: 122364

You are creating a registry on port 8080 but then attempting to rebind your object in the default registry on port 1099. You need to either:

  • pass a full URL to Naming.rebind including the port number, i.e. //localhost:8080/MathServer or
  • save a reference to the Registry object returned by LocateRegistry.createRegistry and bind the object using that registry's instance methods rather than the static methods of Naming

Similarly, you will need to use the full //localhost:8080/MathServer URL form in your clients when they lookup the object, to make sure they're talking to the right registry.

Upvotes: 1

user207421
user207421

Reputation: 310850

Naming.rebind("MathServer", mathServer);

Change that to

Naming.rebind("//localhost:8080/MathServer", mathServer);

Upvotes: 1

Jayesh
Jayesh

Reputation: 101

If you are linux Ubuntu user then while starting RMI registry use command

rmiregistry 8080 &

For Windows

start rmiregistry 8080

Upvotes: 0

Related Questions