[英]java ServerSocket does not throw IOException when bind to the same port
I have an issue with my code as follow. 我的代码有问题,如下所示。 code:
码:
int port = 14205; // begin port
// Try to bind socket on any port<br>
while(true){
try {
InetSocketAddress isa = new InetSocketAddress(port);
Log.logFile("bind port = " + port);
// ss is a ServerSocket object
ss.bind( isa );
break;
} catch (IOException e) {
Log.logFile("exception");
port++;
}
}
I ran this code on JVM 1.7 and JVM 1.6 at the same time on the same machine (win7x64). 我在同一台计算机(win7x64)上同时在JVM 1.7和JVM 1.6上运行了此代码。 using CurrPorts tool to investigate what port we are listening.
使用CurrPorts工具调查我们正在监听的端口。 result: we'are listening on the same port 14207. I think JVM must throw an IOException when run 2nd instance of this code but it didn't.
结果:我们在同一个端口14207上侦听。我认为JVM在运行此代码的第二个实例时必须抛出IOException,但事实并非如此。
what wrong with my code? 我的代码有什么问题? please help.
请帮忙。 note: I must run in this senario (2 JVMs).
注意:我必须在此senario中运行(2个JVM)。
break;
will break your while loop and exit from the while loop and no question about catching exception
. 将打破你的while循环并从while循环退出,毫无疑问会捕获
exception
。 However, When your are trying to catch the exception you need to print exception message to log file. 但是,当您尝试捕获异常时,您需要将异常消息打印到日志文件中。
replace this 取代这个
Log.logFile("exception");
with this 有了这个
Log.logFile("exception"+ e.toString());
第一次绑定后,循环中断。
ok! 好! I will answer my question.
我会回答我的问题。 this is JVM bug.
这是JVM错误。 for more information please refer to Oracle bugbase: http://bugs.sun.com/bugdatabase/view_bug.do;jsessionid=71f70e71266a551ce53dcda4a050c?bug_id=7174699
有关更多信息,请参考Oracle错误库: http ://bugs.sun.com/bugdatabase/view_bug.do;jsessionid=71f70e71266a551ce53dcda4a050c?bug_id=7174699
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.