My program TestBind0 (code below) tries to find/create a registry and bind an object.
Find/create: it first tests if there is already a registry on that port; if not, then it tries to create one.
The program tries to find/create the registry on ports 40654, 50876, 30321, 33445, 1099, in this order, until it succeeds in both creating the registry and binding the object.
Why does TestBind0 throw for each attempt
java.rmi.ConnectException: Connection refused to host: 192.168.1.64; nested exception is:
java.net.ConnectException: Connection refused: connect
at sun.rmi.transport.tcp.TCPEndpoint.newSocket(TCPEndpoint.java:601)
at sun.rmi.transport.tcp.TCPChannel.createConnection(TCPChannel.java:198)
at sun.rmi.transport.tcp.TCPChannel.newConnection(TCPChannel.java:184)
at sun.rmi.server.UnicastRef.newCall(UnicastRef.java:322)
at sun.rmi.registry.RegistryImpl_Stub.rebind(Unknown Source)
at TestBind0.tryPort(TestBind0.java:48)
at TestBind0.main(TestBind0.java:21)
Caused by: java.net.ConnectException: Connection refused: connect
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:333)
at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:195)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:182)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:366)
at java.net.Socket.connect(Socket.java:519)
at java.net.Socket.connect(Socket.java:469)
at java.net.Socket.(Socket.java:366)
at java.net.Socket.(Socket.java:180)
at sun.rmi.transport.proxy.RMIDirectSocketFactory.createSocket(RMIDirectSocketFactory.java:22)
at sun.rmi.transport.proxy.RMIMasterSocketFactory.createSocket(RMIMasterSocketFactory.java:128)
at sun.rmi.transport.tcp.TCPEndpoint.newSocket(TCPEndpoint.java:595)
... 6 more
(note: 192.168.1.64 is my localhost)
in
reg.rebind("TestBind0", obj);
even when I have specified -Djava.security.policy==all.policy , with file all.policy in the current dir, containing
grant {
permission java.security.AllPermission;
};
The program is run using command
java -cp bin -Djava.security.policy==all.policy TestBind0
The code:
import java.rmi.*;
import java.rmi.registry.*;
import java.rmi.server.*;
import java.util.*;
public class TestBind0 extends UnicastRemoteObject implements Counter {
private static final long serialVersionUID = 1L;
protected int count;
protected TestBind0() throws RemoteException {
super();
count = 0;
}
public static void main(String[] args) {
List ports = Arrays.asList(40654, 50876, 30321, 33445, 1099);
Integer successPort = null;
for(int port : ports) {
boolean success = tryPort(port);
if(success) {
successPort = port;
break;
}
}
System.out.println("successPort:" + successPort);
System.out.flush();
}
public static boolean tryPort(int port) {
Registry reg = null;
try {
reg = LocateRegistry.getRegistry(port);
}
catch (RemoteException e) {
try {
reg = LocateRegistry.createRegistry(port);
}
catch (RemoteException e1) {
}
}
if(reg==null) {
return false;
}
boolean success = false;
try {
TestBind0 obj = new TestBind0();
reg.rebind("TestBind0", obj); // line 48
success = true;
}
catch (AccessException e) {
e.printStackTrace();
}
catch (RemoteException e) {
e.printStackTrace();
}
return success;
}
public int getCount() throws RemoteException {
return count++;
}
}