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

(so) SocketChannel.socket().getInetAddress() returns null after close

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Duplicate
    • Icon: P4 P4
    • None
    • 1.4.1, 1.4.2
    • core-libs
    • generic, x86
    • generic, windows_nt

      Name: nt126004 Date: 07/25/2002


      FULL PRODUCT VERSION :
      Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.1-beta-b14)
      Java HotSpot(TM) Client VM (build 1.4.1-beta-b14, mixed mode)

      FULL OPERATING SYSTEM VERSION :
      Windows NT Version 4.0

      A DESCRIPTION OF THE PROBLEM :
      It is unclear from the documentation of Socket whether
      getInetAddress should return null if a socket is closed.
      Presumably closed implies "not connected"--which means null
      should be returned--but that is not obvious.

      With Java 1.4.0, getInetAddress() never returns null. But
      with Java 1.4.1-beta, getInetAddress() returns null if the
      socket was created from a channel and then closed; it does
      not return null if the socket was created with the old
      host/port constructor and then closed. This could be
      considered a bug or just a documentation issue.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Run the included source code with Java 1.4.0 and 1.4.1-beta
      and look at the output. You will need a working network
      connection to establish the sockets.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      In the example below, I run the test with Java 1.4.1-beta
      and 1.4.0. It's not clear whether null should be printed in
      the "Channel socket address" line.

      % java141 -version
      java version "1.4.1-beta"
      Java(TM) 2 Runtime Environment, Standard Edition (build
      1.4.1-beta-b14)
      Java HotSpot(TM) Client VM (build 1.4.1-beta-b14, mixed mode)
      % java141 UnboundSocketInetAddress
      Simple socket address: www.limewire.com/64.61.25.138
      Simple channel address: www.limewire.com/64.61.25.138
      Channel socket address: www.limewire.com/64.61.25.138
      Channel socket address: null

      % java140 -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)
      % java140 UnboundSocketInetAddress
      Simple socket address: www.limewire.com/64.61.25.138
      Simple channel address: www.limewire.com/64.61.25.138
      Channel socket address: www.limewire.com/64.61.25.138
      Channel socket address: www.limewire.com/64.61.25.138

      REPRODUCIBILITY :
      This bug can be reproduced always.

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

      /**
       * Tries to figure out under what conditions Socket.getInetAddress returns null.
       */
      public class UnboundSocketInetAddress {
          public static void main(String args[]) {
              //Create socket with old-fashioned constructor.
              try {
                  Socket socket=new Socket("www.limewire.com", 80);
                  System.out.println("Simple socket address: "
                                     +socket.getInetAddress()); //non-null
                  socket.close();
                  System.out.println("Simple channel address: "
                                     +socket.getInetAddress()); //non-null
              } catch (IOException e) {
                  System.out.println("Couldn't create socket");
              }

              //Create socket from channel.
              try {
                  SocketAddress addr=new InetSocketAddress("www.limewire.com", 80);
                  SocketChannel channel=SocketChannel.open(addr);
                  Socket socket=channel.socket();
                  System.out.println("Channel socket address: "
                                     +socket.getInetAddress()); //non-null
                  socket.close();
                  System.out.println("Channel socket address: "
                                     +socket.getInetAddress()); //NULL on 1.4.1
              } catch (IOException e) {
                  System.out.println("Couldn't create socket");
              }
              System.out.println();
          }
      }

      ---------- END SOURCE ----------

      CUSTOMER WORKAROUND :
      Be prepared for null when calling getInetAddress().
      Or don't call getInetAddress() if the stream is closed.
      (Review ID: 159833)
      ======================================================================

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

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: