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

Extremely slow socket creation using new Socket("ip-address", port)

    XMLWordPrintable

Details

    • Enhancement
    • Resolution: Fixed
    • P2
    • 6
    • 1.4.2, 5.0, 5.0u5
    • core-libs
    • beta
    • x86
    • linux, windows_nt, windows_2000, windows_xp

    Backports

      Description

        Name: gm110360 Date: 08/25/2004


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

        ADDITIONAL OS VERSION INFORMATION :
        Windows XP professional SP1

        EXTRA RELEVANT SYSTEM CONFIGURATION :
        simple LAN network, 1router, 1 PC, router contains DNS, DHCP, etc. router connected to Internet

        A DESCRIPTION OF THE PROBLEM :
        The creation of a socket using new Socket("ip-address", port) seems to be extremely slow. On my machine it takes up to 5 seconds to create the socket. Multiple sockets (in sequence, not parallel) take approximately the same amount of time.



        STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
        Run the provided sample code on java1.5.0 on Windows XP Professional SP1.



        EXPECTED VERSUS ACTUAL BEHAVIOR :
        EXPECTED -
        I expected the time to open sockets to be very small (<10ms).
        ACTUAL -
        Output of the sample code in Java1.5.0:
        Time to open socket (ms): 4515
        Time to open socket (ms): 4500
        Time to open socket (ms): 4500
        Time to open socket (ms): 4500
        Time to open socket (ms): 4500

        Output of the same sample code in Java1.4.2_05:
        Time to open socket (ms): 31
        Time to open socket (ms): 0
        Time to open socket (ms): 0
        Time to open socket (ms): 0
        Time to open socket (ms): 16


        ERROR MESSAGES/STACK TRACES THAT OCCUR :
        no errors, just slow responses

        REPRODUCIBILITY :
        This bug can be reproduced always.

        ---------- BEGIN SOURCE ----------
        for(int i=0;i<5;i++)
        {
            long time_start = System.currentTimeMillis();
            Socket s = new Socket("192.168.1.1",80);
            long time_end = System.currentTimeMillis();
            System.out.println("Time to open socket (ms): "+(time_end-time_start));
            s.close();
        }
        ---------- END SOURCE ----------

        CUSTOMER SUBMITTED WORKAROUND :
        A profiler showed that the problem may not lie in the socket implementation but in the InetAddress implementation because most of the waiting time was spent there.

        A workaround was by creating a InetAddress myself, cache it, and pass that to the constructor of the sockets. This resulted in the behaviour that the first socket took about 4.5 seconds to be created, the others were much faster. Perhaps the bug has something to do with DNS resolving? Note that I do not pass a DNS name to the Socket constructor but a String representation of a IPv4 address. It is not a bug of the router, as I have tried multiple routers (from different vendors).

        I use the sockets to do some HTTP requests. Using HttpUrlConnection to get the HTTP contents made my program much much faster. This makes me believe that the error is not in my OS, but in the implementation of the java Socket classes. There is something in the construction of a socket that is not in the HttpUrlConnection that makes socket creation very slow.
        (Incident Review ID: 297298)
        ======================================================================

        Attachments

          Issue Links

            Activity

              People

                jccollet Jean-Christophe Collet (Inactive)
                gmanwanisunw Girish Manwani (Inactive)
                Votes:
                0 Vote for this issue
                Watchers:
                0 Start watching this issue

                Dates

                  Created:
                  Updated:
                  Resolved:
                  Imported:
                  Indexed: