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

(so) SocketChannel.socket().getLocalPort() returns 0 after bind (lnx)

    Details

    • Subcomponent:
    • Resolved In Build:
      b59
    • CPU:
      x86
    • OS:
      linux, windows_2000

      Description

      Name: rmT116609 Date: 08/29/2002


      FULL PRODUCT VERSION :
      java version "1.4.0"
      Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.0-b92)
      Java HotSpot(TM) Client VM (build 1.4.0-b92, mixed mode)

      also:

      java version "1.4.1-rc"
      Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.1-rc-b19)
      Java HotSpot(TM) Client VM (build 1.4.1-rc-b19, mixed mode)

      FULL OPERATING SYSTEM VERSION :
      glibc-2.2.5-34
      Linux 2.4.18-3
      Red Hat Linux release 7.3 (Valhalla)

      ADDITIONAL OPERATING SYSTEMS :
      Also occurs on Win2K and WinNT


      A DESCRIPTION OF THE PROBLEM :
      The getLocalPort method returns 0 (after calling bind) for SocketChannel Sockets. This method works for Sockets obtained using new Socket(). getLocalAddress doesn't work for SocketChannel Sockets either.

      I don't see the same problem with DatagramSockets.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      1.Obtain a SocketChannel
      2.Grab the Socket from the SocketChannel
      3.Bind the Socket
      4.Determine the LocalPort for the bound socket

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      I expect to get the actual port number. The actual result is 0.

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      import java.net.*;
      import java.nio.channels.*;

      public class Example {

        public static void main(String[] args) throws Exception {
          SocketChannel c = SocketChannel.open();
          Socket s = c.socket();
          s.bind(null);
          int port = s.getLocalPort(); // returns 0 ...
          System.out.println(port);

          Socket s2 = new Socket();
          s2.bind(null);
          int port2 = s2.getLocalPort(); // returns the port
          System.out.println(port2);
        }
      }
      ---------- END SOURCE ----------

      CUSTOMER WORKAROUND :
      I need to know the port number before connecting, so the only work-around is to not use nio, i.e., fallback to blocking Socket IO.
      (Review ID: 163743)
      ======================================================================

        Attachments

          Activity

            People

            • Assignee:
              sherman Xueming Shen
              Reporter:
              rmandalasunw Ranjith Mandala (Inactive)
            • Votes:
              0 Vote for this issue
              Watchers:
              0 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved:
                Imported:
                Indexed: