Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-4492308

ServerSocket BindException when reusing port on Windows

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Duplicate
    • Icon: P3 P3
    • None
    • 1.4.0
    • core-libs
    • x86
    • windows_2000



      Name: nt126004 Date: 08/15/2001


      java version "1.4.0-beta"
      Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.0-beta-b65)
      Java HotSpot(TM) Client VM (build 1.4.0-beta-b65, mixed mode)

      If a serverSocket is created, then accept some connections from client side. If
      the client sides close all the connections before serverSocket.close(),
      everything is fine. But if the server side wants to close the serverSocket
      without notifying the client, after closing all the connections, it will
      produce some problem. Although no exeption is thrown, however, if the program
      creates another serverSocket on the same port, errors about "Address already in
      Used: JVM_Bind" will be thrown. Looks like the port has not been released by
      the serverSocket.close() method. However, this bug only exists in JDK1.4, for
      JDK1.3.0_02 or JDK1.2, it is no problem.

      The SOURCE CODE is as follows: the server creates a serverSocket, accepts five
      connections from client thread, closes each connection just after it is
      created, then closes the serverSocket, waits for two seconds, then creates the
      serverSocket again:

      import java.io.*;
      import java.net.*;

      public class Testing {

      private ServerSocket server;

         public Testing() {
      TestingClient tc=new TestingClient();
            tc.start();
      }

      public void runServer() {
      System.out.println("Server starting");
      try {
      server=new ServerSocket(4000);
      System.out.println( "Server Socket successfully created!" );
      for (int ii=0; ii<3; ii++) {
      Socket connection = server.accept();
      System.out.println("Server: new connection accepted remote port="+connection.getPort());
      connection.close();
      }
      server.close();
      Thread.sleep(2000);
      server=new ServerSocket(4000);
      System.out.println( "Server Socket successfully created again!" );
      server.close();
      } catch (Exception e) { e.printStackTrace(); }
      }

      public static void main (String[] ppppp) {
      Testing testing=new Testing();
      testing.runServer();
      }

      }


      import java.io.*;
      import java.net.*;

      class TestingClient extends Thread {

      Socket clientSocket;

      public void run() {
      try {
      sleep(5000);
      System.out.println("Client starting");

      for (int ii=0; ii<3; ii++) {
      clientSocket = new Socket(InetAddress.getByName("localhost"),4000);
      System.out.println("Client: socket "+(ii+1)+"created "+clientSocket.getLocalPort());
      }
      } catch (Exception e) {e.printStackTrace();}

      }
      }


      The trace information is as follows:

      For JDK 1.4:

      C:\temp>\jdk1.4\jre\bin\java Testing
      Server starting
      Server Socket successfully created!
      Client starting
      Server: new connection accepted remote port =1478
      Client: socket 1 created 1478
      Server: new connection accepted remote port =1479
      Client: socket 2 created 1479
      Server: new connection accepted remote port =1480
      Client: socket 3 created 1480
      java.net.BindException: Address already in use: JVM_Bind
              at java.net.PlainSocketImpl.socketBind(Native Method)
              at java.net.PlainSocketImpl.bind(PlainSocketImpl.java:310)
              at java.net.ServerSocket.bind(ServerSocket.java:305)
              at java.net.ServerSocket.bind(ServerSocket.java:265)
              at java.net.ServerSocket.<init>(ServerSocket.java:201)
              at java.net.ServerSocket.<init>(ServerSocket.java:102)
              at Testing.runServer(Testing.java:29)
              at Testing.main(Testing.java:40)

      For JDK 1.3.0_02:

      C:\temp>\jdk1.3.0_02\bin\java Testing
      BackupServer constructor
      Server starting
      Server Socket successfully created!
      Client starting
      Server: new connection accepted remote port =1466
      Client: socket 1 created 1466
      Server: new connection accepted remote port =1467
      Client: socket 2 created 1467
      Client: socket 3 created 1468
      Server: new connection accepted remote port =1468
      Server Socket successfully created again!

      Additional: if the client closes all the connections before the server call
      serverSocket.close(), it will be fine.
      (Review ID: 130040)
      ======================================================================

            alanb Alan Bateman
            nthompsosunw Nathanael Thompson (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: