在 Java 工作多年后,我第一次接触 RMI,我正在按照本教程了解基础知识。下面的服务器代码给了我以下异常,说端口 1099 已经在使用中,但是查看 netstat -ano 的输出(在 Windows 上),没有进程正在使用 1099。我可以用任何其他未使用的端口替换 1099,它按预期工作,我的客户端程序能够成功连接。
我搜索了一段时间,这是我能找到的最接近有同样问题的人的东西,但他似乎找到了 netstat 的违规进程,而我却找不到。
我在 Netbeans 中进行开发,无论是通过 Netbeans 运行还是自行执行 JAR,我都会遇到相同的异常。我没有像他似乎在做的那样从命令提示符启动注册表,但这似乎没有必要,因为如果我只是更改端口号,一切都会好起来的。
我在这里想念什么?
服务器代码:
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();
}
}
}
例外:
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
更新:
更明确地说,这是netstat -anp tcp
在运行服务器并获得异常之前的完整输出:
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
更新: 我只是对正在使用的端口而不出现在 netstat (不是 RMI 特定)的想法做了更多的研究,但这是唯一看起来相似的东西,而且它不是很有帮助。有没有其他人遇到过这种事情?