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

java.net.Socket cannot connect to java.net.InetAddress.getLocalHost() if hostname is a number

    Details

    • Subcomponent:
    • CPU:
      x86_64
    • OS:
      linux

      Description

      FULL PRODUCT VERSION :
      java version "9.0.1"
      Java(TM) SE Runtime Environment (build 9.0.1+11)
      Java HotSpot(TM) 64-Bit Server VM (build 9.0.1+11, mixed mode)

      ADDITIONAL OS VERSION INFORMATION :
      Linux 123 4.13.11-1-ARCH #1 SMP PREEMPT Thu Nov 2 10:25:56 CET 2017 x86_64 GNU/Linux

      EXTRA RELEVANT SYSTEM CONFIGURATION :
      hostname is a number

      A DESCRIPTION OF THE PROBLEM :
      new java.net.Socket(java.net.InetAddress.getLocalHost(), 8080);

      If the hostname is a number which is allowed by RFC 1123 the following exception will be raised :

      java.net.SocketException thrown: Invalid argument or cannot assign requested address
      | at PlainSocketImpl.socketConnect (Native Method)
      | at AbstractPlainSocketImpl.doConnect (AbstractPlainSocketImpl.java:400)
      | at AbstractPlainSocketImpl.connectToAddress (AbstractPlainSocketImpl.java:243)
      | at AbstractPlainSocketImpl.connect (AbstractPlainSocketImpl.java:225)
      | at SocksSocketImpl.connect (SocksSocketImpl.java:402)
      | at Socket.connect (Socket.java:591)
      | at Socket.connect (Socket.java:540)
      | at Socket.<init> (Socket.java:436)
      | at Socket.<init> (Socket.java:246)
      | at (#3:1)

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      1) Set a number as a hostname :
      #hostname 123

      2)Open a port on the machine :
      python -m http.server 8080

      2)in jshell :
      new java.net.Socket(java.net.InetAddress.getLocalHost(), 8080);
      #################################
      I got the same result with the following class on :
      Java(TM) SE Runtime Environment (build 1.8.0_144-b01)

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

      public class TestHostName {

      public static void main(String[] args) {
      try {
      System.out.println("hostname: " + java.net.InetAddress.getLocalHost());
      Socket socket = new Socket(java.net.InetAddress.getLocalHost(), 8080);
      } catch (IOException e) {
      System.err.println(e);
      }
      }

      }

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      The connection should be established even with a number as a hostname.
      ACTUAL -
      From jshell :

      java.net.SocketException thrown: Invalid argument or cannot assign requested address
      | at PlainSocketImpl.socketConnect (Native Method)
      | at AbstractPlainSocketImpl.doConnect (AbstractPlainSocketImpl.java:400)
      | at AbstractPlainSocketImpl.connectToAddress (AbstractPlainSocketImpl.java:243)
      | at AbstractPlainSocketImpl.connect (AbstractPlainSocketImpl.java:225)
      | at SocksSocketImpl.connect (SocksSocketImpl.java:402)
      | at Socket.connect (Socket.java:591)
      | at Socket.connect (Socket.java:540)
      | at Socket.<init> (Socket.java:436)
      | at Socket.<init> (Socket.java:246)
      | at (#3:1)

      ERROR MESSAGES/STACK TRACES THAT OCCUR :
      java.net.SocketException thrown: Invalid argument or cannot assign requested address
      | at PlainSocketImpl.socketConnect (Native Method)
      | at AbstractPlainSocketImpl.doConnect (AbstractPlainSocketImpl.java:400)
      | at AbstractPlainSocketImpl.connectToAddress (AbstractPlainSocketImpl.java:243)
      | at AbstractPlainSocketImpl.connect (AbstractPlainSocketImpl.java:225)
      | at SocksSocketImpl.connect (SocksSocketImpl.java:402)
      | at Socket.connect (Socket.java:591)
      | at Socket.connect (Socket.java:540)
      | at Socket.<init> (Socket.java:436)
      | at Socket.<init> (Socket.java:246)
      | at (#3:1)

      REPRODUCIBILITY :
      This bug can be reproduced always.

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

      public class TestHostName {

      public static void main(String[] args) {
      try {
      System.out.println("hostname: " + java.net.InetAddress.getLocalHost());
      Socket socket = new Socket(java.net.InetAddress.getLocalHost(), 8080);
      } catch (IOException e) {
      System.err.println(e);
      }
      }

      }
      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      Set a hostname with only letters

        Attachments

          Activity

            People

            • Assignee:
              chegar Chris Hegarty
              Reporter:
              webbuggrp Webbug Group
            • Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

              Dates

              • Created:
                Updated: