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

Binding and connecting to local socket server fails with connection refused

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Incomplete
    • Icon: P4 P4
    • None
    • 7u67
    • core-libs
    • x86
    • other

      FULL PRODUCT VERSION :
      $ java -version
      java version "1.7.0_67"
      Java(TM) SE Runtime Environment (build 1.7.0_67-b01)
      Java HotSpot(TM) 64-Bit Server VM (build 24.65-b04, mixed mode)

      ADDITIONAL OS VERSION INFORMATION :
      $ uname -a
      Darwin dhcp246.sulake.com 13.4.0 Darwin Kernel Version 13.4.0: Sun Aug 17 19:50:11 PDT 2014; root:xnu-2422.115.4~1/RELEASE_X86_64 x86_64

      EXTRA RELEVANT SYSTEM CONFIGURATION :
      $ ifconfig
      lo0: flags=8049<UP,LOOPBACK,RUNNING,MULTICAST> mtu 16384
              options=3<RXCSUM,TXCSUM>
              inet6 ::1 prefixlen 128
              inet 127.0.0.1 netmask 0xff000000
              inet6 fe80::1%lo0 prefixlen 64 scopeid 0x1
              nd6 options=1<PERFORMNUD>
      gif0: flags=8010<POINTOPOINT,MULTICAST> mtu 1280
      stf0: flags=0<> mtu 1280
      en0: flags=8863<UP,BROADCAST,SMART,RUNNING,SIMPLEX,MULTICAST> mtu 1500
              options=10b<RXCSUM,TXCSUM,VLAN_HWTAGGING,AV>
              ether 0c:4d:e9:c3:55:59
              inet6 fe80::e4d:e9ff:fec3:5559%en0 prefixlen 64 scopeid 0x4
              inet 193.94.143.33 netmask 0xffffff00 broadcast 193.94.143.255
              nd6 options=1<PERFORMNUD>
              media: autoselect (1000baseT <full-duplex>)
              status: active
      en1: flags=8823<UP,BROADCAST,SMART,SIMPLEX,MULTICAST> mtu 1500
              ether a8:8e:24:a4:7a:01
              nd6 options=1<PERFORMNUD>
              media: autoselect (<unknown type>)
              status: inactive
      fw0: flags=8863<UP,BROADCAST,SMART,RUNNING,SIMPLEX,MULTICAST> mtu 4078
              lladdr 28:0b:5c:ff:fe:1f:bc:c4
              nd6 options=1<PERFORMNUD>
              media: autoselect <full-duplex>
              status: inactive
      en2: flags=8963<UP,BROADCAST,SMART,RUNNING,PROMISC,SIMPLEX,MULTICAST> mtu 1500
              options=60<TSO4,TSO6>
              ether 32:00:11:fb:cc:40
              media: autoselect <full-duplex>
              status: inactive
      bridge0: flags=8863<UP,BROADCAST,SMART,RUNNING,SIMPLEX,MULTICAST> mtu 1500
              options=63<RXCSUM,TXCSUM,TSO4,TSO6>
              ether 0e:4d:e9:3c:0c:00
              Configuration:
                      id 0:0:0:0:0:0 priority 0 hellotime 0 fwddelay 0
                      maxage 0 holdcnt 0 proto stp maxaddr 100 timeout 1200
                      root id 0:0:0:0:0:0 priority 0 ifcost 0 port 0
                      ipfilter disabled flags 0x2
              member: en2 flags=3<LEARNING,DISCOVER>
                      ifmaxaddr 0 port 7 priority 0 path cost 0
              nd6 options=1<PERFORMNUD>
              media: <unknown type>
              status: inactive
      p2p0: flags=8802<BROADCAST,SIMPLEX,MULTICAST> mtu 2304
              ether 0a:8e:24:a4:7a:01
              media: autoselect
              status: inactive

      $ cat /etc/hosts
      ##
      # Host Database
      #
      # localhost is used to configure the loopback interface
      # when the system is booting. Do not change this entry.
      ##
      127.0.0.1 localhost
      255.255.255.255 broadcasthost
      ::1 localhost
      fe80::1%lo0 localhost


      A DESCRIPTION OF THE PROBLEM :
      When attempting to create a local server and connect to it on some dual stack systems the client fails to connect as it attempts to use the incorrect stack.

      dhcp246:dual continuous-h4k$ javac -cp . DualStackBug.java
      dhcp246:dual continuous-h4k$ java -cp . DualStackBug
      Exception in thread "main" java.net.SocketException: Socket closed
              at java.net.PlainSocketImpl.socketAccept(Native Method)
              at java.net.AbstractPlainSocketImpl.accept(AbstractPlainSocketImpl.java:398)
              at java.net.ServerSocket.implAccept(ServerSocket.java:530)
              at java.net.ServerSocket.accept(ServerSocket.java:498)
              at DualStackBug$1.run(DualStackBug.java:22)
              at java.lang.Thread.run(Thread.java:745)
      java.net.ConnectException: Connection refused
              at java.net.PlainSocketImpl.socketConnect(Native Method)
              at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:339)
              at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:198)
              at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:182)
              at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
              at java.net.Socket.connect(Socket.java:579)
              at java.net.Socket.connect(Socket.java:528)
              at DualStackBug.<init>(DualStackBug.java:34)
              at DualStackBug.main(DualStackBug.java:46)


      ADDITIONAL REGRESSION INFORMATION:
      $ java -version
      java version "1.7.0_67"
      Java(TM) SE Runtime Environment (build 1.7.0_67-b01)
      Java HotSpot(TM) 64-Bit Server VM (build 24.65-b04, mixed mode)

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Run the attached test case on OSX.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      When creating a socket with an ephemeral port on localhost using java.net.ServerSocket#getLocalSocketAddress to connect should work on all systems.
      ACTUAL -
      Attempting to connect using java.net.ServerSocket#getLocalSocketAddress results with java.net.ConnectException: Connection refused.

      ERROR MESSAGES/STACK TRACES THAT OCCUR :
      Exception in thread "main" java.net.SocketException: Socket closed
              at java.net.PlainSocketImpl.socketAccept(Native Method)
              at java.net.AbstractPlainSocketImpl.accept(AbstractPlainSocketImpl.java:398)
              at java.net.ServerSocket.implAccept(ServerSocket.java:530)
              at java.net.ServerSocket.accept(ServerSocket.java:498)
              at DualStackBug$1.run(DualStackBug.java:22)
              at java.lang.Thread.run(Thread.java:745)
      java.net.ConnectException: Connection refused
              at java.net.PlainSocketImpl.socketConnect(Native Method)
              at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:339)
              at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:198)
              at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:182)
              at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
              at java.net.Socket.connect(Socket.java:579)
              at java.net.Socket.connect(Socket.java:528)
              at DualStackBug.<init>(DualStackBug.java:34)
              at DualStackBug.main(DualStackBug.java:46)


      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      import java.io.BufferedReader;
      import java.io.DataOutputStream;
      import java.io.IOException;
      import java.io.InputStreamReader;
      import java.net.*;
      import java.util.concurrent.CountDownLatch;
      import java.util.concurrent.TimeUnit;

      /**
       * @author Johno Crawford (johno@sulake.com)
       */
      public class DualStackBug {

          public DualStackBug() throws IOException, InterruptedException {
              final CountDownLatch latch = new CountDownLatch(1);

              final ServerSocket serverSocket = new ServerSocket(0);
              new Thread(new Runnable() {
                  @Override
                  public void run() {
                      try {
                          Socket connectionSocket = serverSocket.accept();
                          BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(connectionSocket.getInputStream()));
                          System.out.println("Received: " + bufferedReader.readLine());
                          latch.countDown();
                      } catch (IOException e) {
                          e.printStackTrace();
                      }
                  }
              }).start();

              try {
                  Socket clientSocket = new Socket();
                  clientSocket.connect(serverSocket.getLocalSocketAddress());
                  DataOutputStream outputStream = new DataOutputStream(clientSocket.getOutputStream());
                  outputStream.writeBytes("huuhaa");
                  clientSocket.close();

                  latch.await(5, TimeUnit.SECONDS);
              } finally {
                  serverSocket.close();
              }
          }

          public static void main(String[] args) throws IOException, InterruptedException {
              new DualStackBug();
          }
      }
      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      Explicitly set the bind host address to "127.0.0.1" instead of null (InetAddress.anyLocalAddress()).

            msheppar Mark Sheppard
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            5 Start watching this issue

              Created:
              Updated:
              Resolved: