?
Solved

Multiple HttpURLConnections hangs IE

Posted on 1999-11-04
15
Medium Priority
?
241 Views
Last Modified: 2010-04-16
I am using the following innocent looking code in an applet to read data from 3 files:
DataInputStream dis1 = null;
DataInputStream dis2 = null;
DataInputStream dis3 = null;
try  
   {
   URL c = getCodeBase();
   String t = c.getHost()+":"+ c.getPort()+"/"+ c.getFile()+"file1.txt";
   URL u1 = new URL("http://" + c.getHost()+":"+ c.getPort()+ c.getFile()+"/file1.txt");
                                    URLConnection uc1 = u1.openConnection();
                                        dis1 = new DataInputStream(new BufferedInputStream(uc1.getInputStream()));                                
                       
String l = dis1.readLine();            
String labelValue = label1.getText();
label1.setText(labelValue + l.substring(0,10));
URL u2 = new URL("http://" + c.getHost()+":"+ c.getPort()+ c.getFile()+"/file2.txt");
                                    URLConnection uc2 = u2.openConnection();
dis2 = new DataInputStream(new BufferedInputStream(uc2.getInputStream()));                                
l = dis2.readLine();            
labelValue = label1.getText();
label1.setText(labelValue + l.substring(0,12));

URL u3 = new URL("http://" + c.getHost()+":"+ c.getPort()+ c.getFile()+"file3.txt");
                                    URLConnection uc3 = u3.openConnection();
// this one never returns.                            
                                    dis3 = new DataInputStream(new BufferedInputStream(uc3.getInputStream()));                                
l = dis3.readLine();            
                                    labelValue = label1.getText();
                                    label1.setText(labelValue + l.substring(0,12));
                                   System.out.println("after third file read");            
                                    System.in.read();
   }
catch(Exception e)
   {
   System.out.println(e);
   }
}

What happens is that in IE the data is never read from the third file. My analysis leads to the fact that IE will not open more than two sockets for URLConnections, and since the first two reads are blocked, I am entering a deadlock situation.

You can see this for yourselves at:
http://gizmoz.zapa.com/HTTPURLConnection/test

It works fine in NS but hangs IE

Any light that anybody can shed on this issue will help a lot!!!

Thank you
0
Comment
Question by:Doron Gill
  • 5
  • 4
  • 4
  • +1
15 Comments
 
LVL 7

Expert Comment

by:vladi21
ID: 2183295
its work fine in my IE5
Got an exception:
java.io.FileNotFoundException: gizmoz.zapa.com:80//users/HTTPURLConnection/components/test//file4.txt

try donwload latest IE or JVM
0
 
LVL 7

Expert Comment

by:vladi21
ID: 2183309
                                   URL u3 = new URL("http://" + c.getHost()+":"+ c.getPort()+ c.getFile()+"file3.txt");

what about "/file3.txt" instead "file3.txt" ?
0
 

Author Comment

by:Doron Gill
ID: 2183458
Thank you vladi21 for your fast reply. The code attached was a sample code. The actual applet running on gizmoz.zapa.com continues to try and read from a fourth file, until this problem eventually occurs. In your case, your browser managed to read three files but could not make it to the fourth.
We are very confident in our analysis. We have specifically seen that IE4 or IE5 will not allocate more than 2 threads for the http connections. If you have ever heard or seen something like this, it will help us to know.
The ioexception that you got was due to the System.in.read()call at the end. Since the file was never opened then we get an ioexception.
The suggestion to use "/file3.txt" rather than will not work. As you can see the first files are accessed in the same manner without a slash.
Thank you
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
LVL 7

Expert Comment

by:vladi21
ID: 2183472
ok post ur last version source code
0
 

Author Comment

by:Doron Gill
ID: 2183524
This is the full unabridged version of the code.
However, it seems to me, that if the problem did not happen to you the first time, I doubt if it will happen again. Like I said, this happened to us with IE5 as well. Maybe you have a different JVM, where the bug was resolved, or maybe it's a configuration issue, but I doubt it.

import java.awt.*;
import java.io.*;
import java.net.*;
import java.applet.*;

/**
 * This class reads PARAM tags from its HTML host page and sets
 * the color and label properties of the applet. Program execution
 * begins with the init() method.
 */
public class Applet1 extends Applet implements Runnable
{
      /**
       * The entry point for the applet.
       */
      public void init()
      {
            initForm();

      //      usePageParams();
            
            // TODO: Add any constructor code after initForm call.
            Thread load = new Thread (this);
            load.start();
      }
      
      //------------------------------------------------------------------------
      //
      //
      //            HTTP get BUG code:
      //            The third file is never opened.
      //
      //
      //
      //
      public void run()
      {
            DataInputStream dis1 = null;
            DataInputStream dis2 = null;
            DataInputStream dis3 = null;
            DataInputStream dis4 = null;
        try  
        {
                  URL c = getCodeBase();
                  
                  URL u1 = new URL("http://" + c.getHost()+":"+ c.getPort()+ c.getFile()+"/file1.txt");
                  URLConnection uc1 = u1.openConnection();
                  dis1 = new DataInputStream(new BufferedInputStream(uc1.getInputStream()));                              
                  readLine(dis1);
                  
                  URL u2 = new URL("http://" + c.getHost()+":"+ c.getPort()+ c.getFile()+"/file2.txt");
                  URLConnection uc2 = u2.openConnection();
                  dis2 = new DataInputStream(new BufferedInputStream(uc2.getInputStream()));                              
                  readLine(dis2);
                  
                        
                  
                  URL u3 = new URL("http://" + c.getHost()+":"+ c.getPort()+ c.getFile()+"file3.txt");
                  URLConnection uc3 = u3.openConnection();
                  
                  // this one never returns.                  
                  dis3 = new DataInputStream(new BufferedInputStream(uc3.getInputStream()));                              
                  readLine(dis3);
                  
                  URL u4 = new URL("http://" + c.getHost()+":"+ c.getPort()+ c.getFile()+"/file4.txt");
                  URLConnection uc4 = u4.openConnection();      
                  dis4 = new DataInputStream(new BufferedInputStream(uc4.getInputStream()));      
                  readLine(dis4);
                  
                  while (true)
                  {
                        readLine(dis1);
                        readLine(dis2);
                        readLine(dis3);
                  
                        System.out.println("after files read");
                  }
                  
                  //System.in.read();
            }
            catch(Exception e)
            {
                  System.out.println("Got an exception: ");
                  System.out.println(e);
            }
      }
                                
      private void readLine(DataInputStream dis)
      {
            try {
                  String l = dis.readLine();      
                  String labelValue = label1.getText();
                  label1.setText(labelValue + l.substring(0,12));
            }
            catch(Exception e)
            {}
      }
      
      
      private      final String labelParam = "label";
//      private      final String backgroundParam = "background";
//      private      final String foregroundParam = "foreground";

      /**
       * Reads parameters from the applet's HTML host and sets applet
       * properties.
       */
      /*private void usePageParams()
      {
            final String defaultLabel = "Default label";
            final String defaultBackground = "C0C0C0";
            final String defaultForeground = "000000";
            String labelValue;
            String backgroundValue;
            String foregroundValue;

            /**
             * Read the <PARAM NAME="label" VALUE="some string">,
             * <PARAM NAME="background" VALUE="rrggbb">,
             * and <PARAM NAME="foreground" VALUE="rrggbb"> tags from
             * the applet's HTML host.
             
            labelValue = getParameter(labelParam);
            backgroundValue = getParameter(backgroundParam);
            foregroundValue = getParameter(foregroundParam);

            if ((labelValue == null) || (backgroundValue == null) ||
                  (foregroundValue == null))
            {
                  /**
                   * There was something wrong with the HTML host tags.
                   * Generate default values.
                  
                  labelValue = defaultLabel;
                  backgroundValue = defaultBackground;
                  foregroundValue = defaultForeground;
            }
            */
            /**
             * Set the applet's string label, background color, and
             * foreground colors.
             */
            //label1.setText(labelValue);
            
      //}

      /**
       * Converts a string formatted as "rrggbb" to an awt.Color object
       */
      private Color stringToColor(String paramValue)
      {
            int red;
            int green;
            int blue;

            red = (Integer.decode("0x" + paramValue.substring(0,2))).intValue();
            green = (Integer.decode("0x" + paramValue.substring(2,4))).intValue();
            blue = (Integer.decode("0x" + paramValue.substring(4,6))).intValue();

            return new Color(red,green,blue);
      }

      /**
       * External interface used by design tools to show properties of an applet.
       */
/*      public String[][] getParameterInfo()
      {
            String[][] info =
            {
                  { labelParam, "String", "Label string to be displayed" },
                  { backgroundParam, "String", "Background color, format \"rrggbb\"" },
                  { foregroundParam, "String", "Foreground color, format \"rrggbb\"" },
            };
            return info;
      }
*/
      Label label1 = new Label();

      /**
       * Intializes values for the applet and its components
       */
      void initForm()
      {
            this.setBackground(Color.lightGray);
            this.setForeground(Color.black);
            //label1.setText("label1");
            this.setLayout(new BorderLayout());
            this.add("North",label1);
      }
}
0
 
LVL 7

Expert Comment

by:vladi21
ID: 2183545
URL u2 = new URL("http://" + c.getHost()+":"+ c.getPort()+ c.getFile()+"/file2.txt");
URL u3 = new URL("http://" + c.getHost()+":"+ c.getPort()+ c.getFile()+"file3.txt");

???
0
 

Author Comment

by:Doron Gill
ID: 2183591
Actually, to be more precise, there is a redundant "/" in the other files. That does not affect anything, though. The extra slash in the file names of 1 & 2 are simply ignored.
Nevertheless your point is well taken, and I have fixed it in the applet on the site.
0
 
LVL 16

Expert Comment

by:heyhey_
ID: 2184423
> We have specifically seen that IE4 or IE5 will not
> allocate more than 2 threads for the http connections

NN allows max 3 simulanteous http connections - fourth connection is blocked until some of the other http connections finishes
0
 
LVL 5

Accepted Solution

by:
Jod earned 800 total points
ID: 2184815
Two connections is max in IE, ref from Sun:

One thing to note when using semi-persistent Java connections is that the HTTP 1.1 spec recommends browsers only allow two simultaneous socket connects to any given host machine and Java URL connections are subject to this limitation. The upshot of this is that if you have two persistent Java connections to a particular host, the rest of the web browser will be unable to connect to that host until you release one of your connections...NN 4 is not HTTP1.1 and IE is 1.1, so NN seems to allow you more connections. IE locks up after two.
0
 

Author Comment

by:Doron Gill
ID: 2189280
A very deep and full-of-insight reply. Not the one we were hoping for, but I cant kill the messenger. A link to the HTTP 1.1 spec where this issue is raised would have made this reply truly formidable!!!
0
 
LVL 16

Expert Comment

by:heyhey_
ID: 2189342
0
 
LVL 5

Expert Comment

by:Jod
ID: 2190210
To be honest I didn't look it up directly from the RFC itself but from www.javasoft.com as I wasn't sure what restrictions NN and IE imposed.


An obvious fix is to try shutting connections (you may need to give them time to clear out).


A less obvious fix is to try setting your IE browser to not use HTTP 1.1. To be honest, I'm not sure whether this will work well and is not a good long term solution as you are still attemptimg concurrent connections, but if you go to:

Tools - Internet Options - Advanced

and deselect HTTP 1.1 (you will probably need to restart the PC)

this may get things working.

Now you are using HTTP 1.0 you will establish a connection for each separate download - which perhaps creates other performance problems for your browser. HTTP 1.1 is supposed to be about 30% faster due to having the ability to keep connections alive.
0
 
LVL 16

Expert Comment

by:heyhey_
ID: 2190490
the only real workwaround id to implement HTTP protocol yourself and use directly java Sockets.
0
 

Author Comment

by:Doron Gill
ID: 2190518
Thanks people for all the input. As for heyhey's comment - the problem with using java sockets is that it doesnt work with a firewall/proxy configuration. This is what we did to begin with but resorted to HTTPURLConnection to overcome the firewall problem
0
 
LVL 16

Expert Comment

by:heyhey_
ID: 2190645
>> the problem with using java sockets is that it doesnt work
>> with a firewall/proxy configuration.

you can implement the HTTP proxy protocol :), but applets are usually not allowed to make socket connection to the proxy ... neither they can find proxy IP address ...

0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Suggested Courses
Course of the Month5 days, 22 hours left to enroll

588 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question