472,804 Members | 761 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,804 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 4063
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

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

Similar topics

by: James Hong | last post by:
Help please, I try to sending an email from my html page using the java applet. but it give error on most of the PC only very few work, what is the error i make the java applet show as below ...
by: Jonathan | last post by:
Hi I'm doing a project for school and wrote an applet that makes a socket connection to a server (smae host as webserver) that was setup for this project. In the applet there are 3 buttons and by...
by: Adam H. Peterson | last post by:
I would like to make a stream or streambuf that tracks the number of lines that have been read and stuff like that (so, for example, when I get an error message, I can ask the stream for the line...
by: kernelxu | last post by:
hi,everybody. I calling function setbuf() to change the characteristic of standsrd input buffer. some fragment of the progrem is: (DEV-C++ #include <stdio.h> #include <stdlib.h> int...
by: ankur | last post by:
WHEN I RUN THIS WEB APPLICATION ON Tomcat5.5.9 SERVER MY HttpChatApplet sccessfully Loaded from ChatDispatch but running on some another PC HttpChatApplet not loaded my Coad ...
by: msewing | last post by:
I'm running 2.5.1 on Fedora 8 ( with the following soundcards: Ensoniq ES1370 (rev 01) VIA Technologies, Inc. VT8233/A/8235/8237 AC97 Audio Controller (rev 50) I'm working on...
by: sheephead86 | last post by:
Hi, I'm pretty new to java, and I have a small problem involving drawing a rectangle on a java applet.Firstly this is not a plea for someone to help me with this peice of work, I just need pointing...
by: Kid Programmer | last post by:
Hello guys. I recently found a thing on my computer called Build Applet. I did a bit of research on it and found out how to use it. I dragged the code of a simple program onto it and it generated...
by: kummu4help | last post by:
hi, i want to draw rectangle based on mousedrag event. if user dragging the mouse, then the rectangle on the applet should increase or decrease basing on current mouse coordinates. i have the...
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
by: erikbower65 | last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps: 1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal. 2. Connect to...
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
by: kcodez | last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Sept 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
by: Mushico | last post by:
How to calculate date of retirement from date of birth
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

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.