JDK-7174699 : No BindException when starting a ServerSocket twice on the same port.
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.net
  • Affected Version: 6u32
  • Priority: P4
  • Status: Closed
  • Resolution: Duplicate
  • OS: windows_7
  • CPU: x86
  • Submitted: 2012-06-06
  • Updated: 2012-07-23
  • Resolved: 2012-06-06
Related Reports
Duplicate :  
Description
FULL PRODUCT VERSION :
java version "1.6.0_32"
Java(TM) SE Runtime Environment (build 1.6.0_32-b05)
Java HotSpot(TM) 64-Bit Server VM (build 20.7-b02, mixed mode)

java version "1.7.0_04"
Java(TM) SE Runtime Environment (build 1.7.0_04-b22)
Java HotSpot(TM) 64-Bit Server VM (build 23.0-b21, mixed mode)

ADDITIONAL OS VERSION INFORMATION :
Microsoft Windows [Version 6.1.7601]

A DESCRIPTION OF THE PROBLEM :
In a program I open a ServerSocket on port 7777 when start this program at the same time again I get a BindException as expected. This happens when I use Java 6 or Java 7. But when I use for Java 6 first to start my program and at the same time I start the same program with Java 7 (or first Java 6 and second Java 7) I don't get a BindException. When I start a client program the first started 'server' handles the incoming request. But when I close the first 'server' and I start the client again the second 'server' handles the incoming request.

STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
-Windows 7 64 bit
-Open CMD and start test program with Java 6 <location jre6>\bin\java test.ServerSocketExample
-Open CMD and start test program with Java 7 <location jre7>\bin\java test.ServerSocketExample

Does not matter if you start the program first with Java 6 or Java 7.


EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
Output of first program
Waiting for client connection on port: 7777

Output of second program
java.net.BindException: Address already in use: JVM_Bind
        at java.net.DualStackPlainSocketImpl.bind0(Native Method)
        at java.net.DualStackPlainSocketImpl.socketBind(Unknown Source)
        at java.net.AbstractPlainSocketImpl.bind(Unknown Source)
        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 test.ServerSocketExample.<init>(ServerSocketExample.java:13)
        at test.ServerSocketExample.main(ServerSocketExample.java:34)
Waiting for client connection on port: 7777
Exception in thread "main" java.lang.NullPointerException
        at test.ServerSocketExample.handleConnection(ServerSocketExample.java:24)
        at test.ServerSocketExample.main(ServerSocketExample.java:35)
ACTUAL -
Output of first program
Waiting for client connection on port: 7777

Output of second program
Waiting for client connection on port: 7777

netstat -a -n

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:1098           0.0.0.0:0              LISTENING
  TCP    0.0.0.0:1099           0.0.0.0:0              LISTENING
  TCP    0.0.0.0:1433           0.0.0.0:0              LISTENING
  TCP    0.0.0.0:1521           0.0.0.0:0              LISTENING
  TCP    0.0.0.0:2099           0.0.0.0:0              LISTENING
  TCP    0.0.0.0:2484           0.0.0.0:0              LISTENING
  TCP    0.0.0.0:3389           0.0.0.0:0              LISTENING
  TCP    0.0.0.0:3528           0.0.0.0:0              LISTENING
  TCP    0.0.0.0:3873           0.0.0.0:0              LISTENING
  TCP    0.0.0.0:4160           0.0.0.0:0              LISTENING
  TCP    0.0.0.0:4444           0.0.0.0:0              LISTENING
  TCP    0.0.0.0:4446           0.0.0.0:0              LISTENING
  TCP    0.0.0.0:4457           0.0.0.0:0              LISTENING
  TCP    0.0.0.0:6002           0.0.0.0:0              LISTENING
  TCP    0.0.0.0:7001           0.0.0.0:0              LISTENING
  TCP    0.0.0.0:7002           0.0.0.0:0              LISTENING
  TCP    0.0.0.0:7777           0.0.0.0:0              LISTENING
  TCP    0.0.0.0:7777           0.0.0.0:0              LISTENING
  TCP    0.0.0.0:8080           0.0.0.0:0              LISTENING
  TCP    0.0.0.0:8081           0.0.0.0:0              LISTENING
  TCP    0.0.0.0:8083           0.0.0.0:0              LISTENING
  TCP    0.0.0.0:8085           0.0.0.0:0              LISTENING
  TCP    0.0.0.0:8086           0.0.0.0:0              LISTENING
  TCP    0.0.0.0:8087           0.0.0.0:0              LISTENING
  TCP    0.0.0.0:8088           0.0.0.0:0              LISTENING
  TCP    0.0.0.0:8181           0.0.0.0:0              LISTENING
  TCP    0.0.0.0:8501           0.0.0.0:0              LISTENING
  TCP    0.0.0.0:9535           0.0.0.0:0              LISTENING
  TCP    0.0.0.0:9593           0.0.0.0:0              LISTENING
  TCP    0.0.0.0:9594           0.0.0.0:0              LISTENING
  TCP    0.0.0.0:9595           0.0.0.0:0              LISTENING
  TCP    0.0.0.0:33354          0.0.0.0:0              LISTENING
  TCP    0.0.0.0:37204          0.0.0.0:0              LISTENING
  TCP    0.0.0.0:44888          0.0.0.0:0              LISTENING
  TCP    0.0.0.0:49152          0.0.0.0:0              LISTENING
  TCP    0.0.0.0:49153          0.0.0.0:0              LISTENING
  TCP    0.0.0.0:49154          0.0.0.0:0              LISTENING
  TCP    0.0.0.0:49214          0.0.0.0:0              LISTENING
  TCP    0.0.0.0:49217          0.0.0.0:0              LISTENING
  TCP    0.0.0.0:49233          0.0.0.0:0              LISTENING
  TCP    0.0.0.0:49234          0.0.0.0:0              LISTENING
  TCP    0.0.0.0:49236          0.0.0.0:0              LISTENING
  TCP    0.0.0.0:49387          0.0.0.0:0              LISTENING
  TCP    0.0.0.0:49388          0.0.0.0:0              LISTENING
  TCP    0.0.0.0:49399          0.0.0.0:0              LISTENING
  TCP    0.0.0.0:49404          0.0.0.0:0              LISTENING
  TCP    10.4.16.101:139        0.0.0.0:0              LISTENING
  TCP    10.4.16.101:1098       10.4.16.101:49397      ESTABLISHED
  TCP    10.4.16.101:3389       10.4.24.4:54862        ESTABLISHED
  TCP    10.4.16.101:8086       10.4.16.101:51915      FIN_WAIT_2
  TCP    10.4.16.101:8501       10.4.16.101:49398      ESTABLISHED
  TCP    10.4.16.101:49214      10.4.16.101:51916      TIME_WAIT
  TCP    10.4.16.101:49397      10.4.16.101:1098       ESTABLISHED
  TCP    10.4.16.101:49398      10.4.16.101:8501       ESTABLISHED
  TCP    10.4.16.101:50160      10.1.8.101:445         ESTABLISHED
  TCP    10.4.16.101:51912      10.4.16.101:1098       TIME_WAIT
  TCP    10.4.16.101:51913      10.4.16.101:49388      TIME_WAIT
  TCP    10.4.16.101:51914      10.4.16.101:1098       TIME_WAIT
  TCP    10.4.16.101:51915      10.4.16.101:8086       CLOSE_WAIT
  TCP    127.0.0.1:1434         0.0.0.0:0              LISTENING
  TCP    127.0.0.1:6999         0.0.0.0:0              LISTENING
  TCP    127.0.0.1:9535         127.0.0.1:50034        ESTABLISHED
  TCP    127.0.0.1:9592         0.0.0.0:0              LISTENING
  TCP    127.0.0.1:21584        0.0.0.0:0              LISTENING
  TCP    127.0.0.1:49203        0.0.0.0:0              LISTENING
  TCP    127.0.0.1:49204        0.0.0.0:0              LISTENING
  TCP    127.0.0.1:49222        0.0.0.0:0              LISTENING
  TCP    127.0.0.1:50034        127.0.0.1:9535         ESTABLISHED
  TCP    127.0.0.1:51917        127.0.0.1:53001        TIME_WAIT
  TCP    127.0.0.1:53001        0.0.0.0:0              LISTENING
  TCP    [::]:135               [::]:0                 LISTENING
  TCP    [::]:445               [::]:0                 LISTENING
  TCP    [::]:1098              [::]:0                 LISTENING
  TCP    [::]:1099              [::]:0                 LISTENING
  TCP    [::]:1433              [::]:0                 LISTENING
  TCP    [::]:2099              [::]:0                 LISTENING
  TCP    [::]:3389              [::]:0                 LISTENING
  TCP    [::]:3528              [::]:0                 LISTENING
  TCP    [::]:3873              [::]:0                 LISTENING
  TCP    [::]:4160              [::]:0                 LISTENING
  TCP    [::]:4444              [::]:0                 LISTENING
  TCP    [::]:4446              [::]:0                 LISTENING
  TCP    [::]:4457              [::]:0                 LISTENING
  TCP    [::]:7777              [::]:0                 LISTENING
  TCP    [::]:7777              [::]:0                 LISTENING
  TCP    [::]:8081              [::]:0                 LISTENING
  TCP    [::]:8083              [::]:0                 LISTENING
  TCP    [::]:8085              [::]:0                 LISTENING
  TCP    [::]:8086              [::]:0                 LISTENING
  TCP    [::]:8087              [::]:0                 LISTENING
  TCP    [::]:8088              [::]:0                 LISTENING
  TCP    [::]:8181              [::]:0                 LISTENING
  TCP    [::]:8501              [::]:0                 LISTENING
  TCP    [::]:37204             [::]:0                 LISTENING
  TCP    [::]:44888             [::]:0                 LISTENING
  TCP    [::]:49152             [::]:0                 LISTENING
  TCP    [::]:49153             [::]:0                 LISTENING
  TCP    [::]:49154             [::]:0                 LISTENING
  TCP    [::]:49214             [::]:0                 LISTENING
  TCP    [::]:49217             [::]:0                 LISTENING
  TCP    [::]:49233             [::]:0                 LISTENING
  TCP    [::]:49234             [::]:0                 LISTENING
  TCP    [::]:49236             [::]:0                 LISTENING
  TCP    [::]:49387             [::]:0                 LISTENING
  TCP    [::]:49388             [::]:0                 LISTENING
  TCP    [::]:49399             [::]:0                 LISTENING
  TCP    [::]:49404             [::]:0                 LISTENING
  TCP    [::1]:1434             [::]:0                 LISTENING
  TCP    [::1]:6999             [::]:0                 LISTENING
  UDP    0.0.0.0:123            *:*
  UDP    0.0.0.0:500            *:*
  UDP    0.0.0.0:1434           *:*
  UDP    0.0.0.0:4160           *:*
  UDP    0.0.0.0:4160           *:*
  UDP    0.0.0.0:4500           *:*
  UDP    0.0.0.0:5355           *:*
  UDP    0.0.0.0:7001           *:*
  UDP    0.0.0.0:9595           *:*
  UDP    0.0.0.0:33354          *:*
  UDP    0.0.0.0:33355          *:*
  UDP    0.0.0.0:38293          *:*
  UDP    0.0.0.0:55932          *:*
  UDP    10.4.16.101:123        *:*
  UDP    10.4.16.101:137        *:*
  UDP    10.4.16.101:138        *:*
  UDP    10.4.16.101:6001       *:*
  UDP    127.0.0.1:123          *:*
  UDP    127.0.0.1:6001         *:*
  UDP    127.0.0.1:53399        *:*
  UDP    127.0.0.1:58903        *:*
  UDP    127.0.0.1:60317        *:*
  UDP    [::]:500               *:*
  UDP    [::]:1434              *:*
  UDP    [::]:4160              *:*
  UDP    [::]:4160              *:*
  UDP    [::]:4500              *:*
  UDP    [::]:55932             *:*

REPRODUCIBILITY :
This bug can be reproduced always.

---------- BEGIN SOURCE ----------
package test;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
 
public class ServerSocketExample {
    private ServerSocket server;
    private int port = 7777;
 
    public ServerSocketExample() {
        try {
            server = new ServerSocket(port);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
 
    public void handleConnection() {
        System.out.println("Waiting for client connection on port: " + port);
 
        while (true) {
            try {
                Socket socket = server.accept();
                socket.close();
                //new ConnectionHandler(socket);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    
    public static void main(String[] args) {
        ServerSocketExample example = new ServerSocketExample();
        example.handleConnection();
    }
---------- END SOURCE ----------