471,049 Members | 1,305 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,049 software developers and data experts.

Applet input stream buffering delay, how to stop?

310 100+
I have an applet that accepts data as a stream from a php script using the following code snippet:

Expand|Select|Wrap|Line Numbers
  1.            String dataendpoint = "http://phpfunction.php";
  3.             URL url = new URL(dataendpoint);
  4.             InputStream is = url.openStream();
  5.             String line;
  6.             InputStreamReader isr = new InputStreamReader(is);
  7.             BufferedReader br = new BufferedReader(isr,0);
  8.             while ((line = br.readLine()) != null) {
  9.                // my code here to handle each line of data which prepares it for
  10.                // showing on a line graph.
  11.             }
The variable dataendpoint actually comes into the applet as a parameter, I am just showing something here for clarity.

The intention is to have PHP read existing datafiles and produce the initial data for this applet which is then plotted on a graph in the browser. After this initial data is shown, the data will then continue to be streamed as new data is produced. So the PHP code is like this (shown as pseudocode):

Expand|Select|Wrap|Line Numbers
  1.    // open data files, read lines, and send to standard output using echo
  2.    // flush data using ob_flush() and flush() calls
  3.    while (1) {
  4.       sleep(30);
  5.       // get new data, if any, then send to standard output
  6.       // flush buffers again
  7.    }
Here is the problem: When I leave off the while loop and just let the script spit out the contents of the datafiles and exit, everything works fine and my graph appears immediately in my applet.

With the while loop, however, nothing appears for a very long time delay. After this time delay, everything works fine, and my graph is updated every 30 seconds when new data appears, just as I want it to.

The problem is how to avoid this long delay time which is occurring? I have done a Wireshark trace and see that the data from the files is being sent immediately to the browser. Putting print statements in my Java code shows me that the code is blocking at this line:

InputStream is = url.openStream();

Can anyone tell me what is happening here and what to do about it?

Thanks for any help!!!

Steve, Denmark
Feb 20 '09 #1
4 3923
11,448 Expert 8TB
Two remarks:

1) what happens when you put that 'sleep(30)' at the end of your while loop?
2) you can't instantiate a BufferedReader with a buffer size of zero (0).

kind regards,

Feb 20 '09 #2
310 100+
Hi Jos,
yes you are correct, the code I showed above does not work because of the buffer size of 0. That was a cut and paste of the code during my experimentation. I found out afterwards that it doesn't work.

Putting the sleep at the end of the infinite while loop is not easy to do as that would mean I have a steady amount of data to send out, which is not the case here (the data comes from another process and the PHP script will poll to see if there is any new data to send, otherwise sleep).

It may be interesting, however, to try to produce fake data to see if the browser and applet reacts after a certain minimum of data has been fed to it. Since I can send comment lines to my applet this would then be one way to get around the problem, if it works (am home now, will try on Monday) but it would be nice if there was a real solution to the problem.

Steve, Denmark
Feb 20 '09 #3
2,057 Expert 2GB
? I don't understand how this is the case, if you put the sleep as the last statement inside the while loop.
With your current setup there is a mandatory 30 second wait even if it is unecessary. Since it isn't returning anything, this may be causing problems on the other side with opening the stream. With Josah's modification, wouldn't it output the data it has, then wait 30 seconds before it checks again?
Feb 20 '09 #4
310 100+
I may be missing something or maybe my explanation of the situation is not so clear. Let me try again. My PHP script opens a data file and dumps the contents of it out to the browser with echo statements. When it reaches the end of the data file the script does not finish and exit. Instead, I wait some time with the sleep() function, then I look to see if there is new data. If there is, I send it also out to the browser and wait again. This is why the sleep here is at the start of the while loop.

The PHP script is just an input stream for the applet in the client browser. It supplies a chunk of data at once, and then some more data whenever data is ready. The applet plots this data as a scrolling time graph.

Now, this works if I have the PHP script exit before doing the while loop, where I then send only the current data in the file to be plotted on the applet. It also works with the while loop, but only after a rather long delay time, where the applet just appears to be waiting for data. After the delay time I get the scrolling update of the applet plot as I want.

My question here is why do I get this delay, which is apparently caused by the url.openstream() call not returning. I am flushing the PHP script data before and during the while loop. I can see from sniffing that the data is indeed sent to the browser. So there is something in the browser or in the Java within the browser that decides that it should wait before letting the Java program get past the openstream() call.

I cannot see why this happens, unless something within the browser is causing a buffering of the input stream before letting the openstream() return.

Can anyone help?
Feb 20 '09 #5

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

reply views Thread by James Hong | last post: by
2 posts views Thread by Jonathan | last post: by
15 posts views Thread by Adam H. Peterson | last post: by
reply views Thread by ankur | last post: by
1 post views Thread by Kid Programmer | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.