I'm getting my feet wet in RMI for the first time after working in Java for a number of years, and I'm following this tutorial to get the basics down. The server code below is giving me the following exception saying that port 1099 is already in use, but looking at the output of netstat -ano (on windows), no process is using 1099. I can replace 1099 with any other unused port and it works as expected, with my client program able to connect successfully.
I searched for a while, and this was the nearest thing I could find to someone having the same problem, but he seems to have found the offending process with netstat, which I cannot.
I'm developing in Netbeans, and I get the same exception whether running through Netbeans or executing the JAR on its own. I'm not starting the registry from the command prompt as he seems to be doing, but that doesn't seem to be necessary, as everything goes just fine if I merely change the port number.
What am I missing here?
Server code:
import java.rmi.Naming;
import java.rmi.registry.LocateRegistry;
public class RMIServer {
public static void main(String[] args) throws InterruptedException {
try {
LocateRegistry.createRegistry(1099);
Naming.rebind("//localhost:1099/MyRemote", new MyRemote());
System.out.println("Server ready.");
} catch (Exception ex) {
System.out.println("Server error:");
ex.printStackTrace();
}
}
}
Exception:
Server error:
java.rmi.server.ExportException: Port already in use: 1099; nested exception is:
java.net.BindException: Address already in use: JVM_Bind
at sun.rmi.transport.tcp.TCPTransport.listen(TCPTransport.java:328)
at sun.rmi.transport.tcp.TCPTransport.exportObject(TCPTransport.java:236)
at sun.rmi.transport.tcp.TCPEndpoint.exportObject(TCPEndpoint.java:411)
at sun.rmi.transport.LiveRef.exportObject(LiveRef.java:147)
at sun.rmi.server.UnicastServerRef.exportObject(UnicastServerRef.java:207)
at sun.rmi.registry.RegistryImpl.setup(RegistryImpl.java:122)
at sun.rmi.registry.RegistryImpl.<init>(RegistryImpl.java:108)
at java.rmi.registry.LocateRegistry.createRegistry(LocateRegistry.java:203)
at rmiserver.RMIServer.main(RMIServer.java:10)
Caused by: java.net.BindException: Address already in use: JVM_Bind
at java.net.TwoStacksPlainSocketImpl.socketBind(Native Method)
at java.net.AbstractPlainSocketImpl.bind(AbstractPlainSocketImpl.java:376)
at java.net.TwoStacksPlainSocketImpl.bind(TwoStacksPlainSocketImpl.java:101)
at java.net.PlainSocketImpl.bind(PlainSocketImpl.java:175)
at java.net.ServerSocket.bind(ServerSocket.java:376)
at java.net.ServerSocket.<init>(ServerSocket.java:237)
at java.net.ServerSocket.<init>(ServerSocket.java:128)
at sun.rmi.transport.proxy.RMIDirectSocketFactory.createServerSocket(RMIDirectSocketFactory.java:45)
at sun.rmi.transport.proxy.RMIMasterSocketFactory.createServerSocket(RMIMasterSocketFactory.java:349)
at sun.rmi.transport.tcp.TCPEndpoint.newServerSocket(TCPEndpoint.java:667)
at sun.rmi.transport.tcp.TCPTransport.listen(TCPTransport.java:317)
... 8 more
Update:
Just to be more explicit, here's the complete output of netstat -anp tcp
just prior to running the server and getting the exception:
C:\>netstat -anp tcp
Active Connections
Proto Local Address Foreign Address State
TCP 0.0.0.0:135 0.0.0.0:0 LISTENING
TCP 0.0.0.0:445 0.0.0.0:0 LISTENING
TCP 0.0.0.0:2002 0.0.0.0:0 LISTENING
TCP 0.0.0.0:5800 0.0.0.0:0 LISTENING
TCP 0.0.0.0:5900 0.0.0.0:0 LISTENING
TCP 0.0.0.0:16992 0.0.0.0:0 LISTENING
TCP 0.0.0.0:16993 0.0.0.0:0 LISTENING
TCP 0.0.0.0:24800 0.0.0.0:0 LISTENING
TCP 127.0.0.1:1027 127.0.0.1:24801 ESTABLISHED
TCP 127.0.0.1:1035 127.0.0.1:2002 ESTABLISHED
TCP 127.0.0.1:1036 0.0.0.0:0 LISTENING
TCP 127.0.0.1:1045 127.0.0.1:24801 ESTABLISHED
TCP 127.0.0.1:1092 0.0.0.0:0 LISTENING
TCP 127.0.0.1:2002 127.0.0.1:1035 ESTABLISHED
TCP 127.0.0.1:5152 0.0.0.0:0 LISTENING
TCP 127.0.0.1:24801 0.0.0.0:0 LISTENING
TCP 127.0.0.1:24801 127.0.0.1:1027 ESTABLISHED
TCP 127.0.0.1:24801 127.0.0.1:1045 ESTABLISHED
TCP 192.168.2.76:139 0.0.0.0:0 LISTENING
TCP 192.168.2.76:1029 64.74.103.175:443 ESTABLISHED
TCP 192.168.2.76:1038 192.168.2.220:445 ESTABLISHED
TCP 192.168.2.76:1042 192.168.2.55:445 ESTABLISHED
TCP 192.168.2.76:1057 74.125.225.40:443 ESTABLISHED
TCP 192.168.2.76:1058 74.125.133.95:443 TIME_WAIT
TCP 192.168.2.76:1060 74.125.142.125:5222 ESTABLISHED
TCP 192.168.2.76:1063 74.125.225.47:443 ESTABLISHED
TCP 192.168.2.76:1064 74.125.225.34:443 ESTABLISHED
TCP 192.168.2.76:1066 74.125.225.47:443 ESTABLISHED
TCP 192.168.2.76:1082 74.125.142.125:5222 ESTABLISHED
TCP 192.168.2.76:1138 74.125.225.32:443 TIME_WAIT
TCP 192.168.2.76:1139 74.125.225.32:443 TIME_WAIT
TCP 192.168.2.76:1140 173.194.46.37:443 ESTABLISHED
TCP 192.168.2.76:1143 91.199.212.171:80 CLOSE_WAIT
TCP 192.168.2.76:1144 178.255.82.1:80 CLOSE_WAIT
TCP 192.168.2.76:1150 74.125.225.32:443 TIME_WAIT
TCP 192.168.2.76:1152 198.252.206.16:80 ESTABLISHED
TCP 192.168.2.76:1153 198.252.206.16:80 ESTABLISHED
TCP 192.168.2.76:24800 192.168.2.52:15713 ESTABLISHED
Update: I did some more research just on the idea of a port being in use without appearing in netstat (not RMI specific), but this was the only thing that seemed similar, and it's not very helpful. Has anyone else encountered this sort of thing before?