Link to home
Start Free TrialLog in
Avatar of Doron Gill
Doron Gill

asked on

Multiple HttpURLConnections hangs IE

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
Avatar of vladi21
vladi21

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
                                   URL u3 = new URL("http://" + c.getHost()+":"+ c.getPort()+ c.getFile()+"file3.txt");

what about "/file3.txt" instead "file3.txt" ?
Avatar of Doron Gill

ASKER

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
ok post ur last version source code
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);
      }
}
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");

???
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.
> 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
ASKER CERTIFIED SOLUTION
Avatar of Jod
Jod

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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!!!
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.
the only real workwaround id to implement HTTP protocol yourself and use directly java Sockets.
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
>> 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 ...