473,405 Members | 2,404 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 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";
  2.  
  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.             }
  12.  
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.    }
  8.  
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 4098
JosAH
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,

Jos
Feb 20 '09 #2
coolsti
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
jkmyoung
2,057 Expert 2GB
@coolsti
? 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
coolsti
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

0
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 ...
2
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...
15
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...
9
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++2.9.9.2) #include <stdio.h> #include <stdlib.h> int...
0
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 ...
1
msewing
by: msewing | last post by:
I'm running 2.5.1 on Fedora 8 (2.6.23.9-85.fc8) with the following soundcards: Ensoniq ES1370 (rev 01) VIA Technologies, Inc. VT8233/A/8235/8237 AC97 Audio Controller (rev 50) I'm working on...
1
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...
1
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...
1
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.