简体   繁体   English

绑定到同一端口时,java ServerSocket不会引发IOException

[英]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.

相关问题 使用ServerSocket绑定已使用的端口时,Java 7不会引发BindException - Java 7 doesn't throw BindException when binding an already used port using ServerSocket ServerSocket.close()。 什么时候抛出异常? - ServerSocket.close(). When does it throw an exception? 为什么Java ServerSocket accept()返回一个与ServerSocket具有相同端口的套​​接字? - Why Java ServerSocket accept() returns a socket with the same port as ServerSocket? java.util.zip.ZipFile.close()什么时候抛出IOException? - when does java.util.zip.ZipFile.close() throw IOException? 在java中为什么FileWriter抛出IOException而FileOutputStream抛出FileNotFoundException的原因完全相同 - In java why does FileWriter throw IOException while FileOutputStream throw FileNotFoundException for the exact same reasons Java ServerSocket在绑定到远程IP地址时抛出IOException - Java ServerSocket throwing an IOException when binding to a remote IP address Java ServerSocket如何在接受客户端后获得绑定到同一本地端口的新套接字? - How does Java ServerSocket get a new socket bound to the same local port after accept the client? 网络断开连接不会在Java中引发IOException - Network disconnect does not throw IOException in Java DatagramSocket.receive()何时会引发IOException? - When does DatagramSocket.receive() throw IOException? serversocket类如何在同一端口上提供多个客户端连接? - How does serversocket class serve multiple client connections on same port?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM