ServerSocket fails upons construction
I have the following construction:
try {
serverSocket = new ServerSocket(port);
} catch (Exception e) {
throw new RuntimeException("Could not create server socket.");
}
and the construction fails and I get the "Could not create server socket" error. The port is always 8085. I opened both out-bounding and in-bounding ports in my (Windows 7) firewall. What could be the problem?
The exception is IOException:
Exception in thread "main" java.lang.RuntimeException: Could not create server socket. at cs236369.proxy.ProxyServer.(ProxyServer.java:23) at cs236369.proxy.ProxyServer.main(ProxyServer.java:62)
Running netstat -an
gives the following result:
TCP 0.0.0.0:8085 0.0.0.0:0 LISTENING
TCP [::]:8085 [::]:0 LISTENING
Stack Trace:
java.ne开发者_StackOverflow中文版t.BindException: Address already in use: JVM_Bind
at java.net.PlainSocketImpl.socketBind(Native Method)
at java.net.PlainSocketImpl.bind(Unknown Source)
at java.net.ServerSocket.bind(Unknown Source)
at java.net.ServerSocket.<init>(Unknown Source)
at java.net.ServerSocket.<init>(Unknown Source)
at cs236369.proxy.ProxyServer.<init>(ProxyServer.java:21)
at cs236369.proxy.ProxyServer.main(ProxyServer.java:63)
According to the API, a BindException
Signals that an error occurred while attempting to bind a socket to a local address and port. Typically, the port is in use, or the requested local address could not be assigned.
Basically, that port could be in use by another process, even perhaps by another instance of the same application due to an unclean shutdown. This link may help you troubleshoot.
Note:
The posted results of running the netstat -an
command seems to verify that port 8085 is in use by multiple processes, which would result in the BindException
that you're encountering.
You'll get a more detailed reason for your exception if you include the caught exception in the newly thrown exception:
try {
serverSocket = new ServerSocket(port);
} catch (Exception e) {
throw new RuntimeException("Could not create server socket.", e);
}
My initial guess, though, is that the port is already in use if you've already dealt with firewall issues.
精彩评论