473,594 Members | 2,747 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Applet input stream buffering delay, how to stop?

310 Contributor
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 4112
JosAH
11,448 Recognized Expert MVP
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 Contributor
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 Recognized Expert Top Contributor
@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 Contributor
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
9866
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 ********************************** package Celcom.Client;
2
3549
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 pressing one of them it triggers a specific query, like getting the server uptime, date or who (is online). The problem is that when I press one of the buttons it send the query to the server, reads from the BufferedReader. This goes all well but...
15
2211
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 number and put it in error messages. I tried to create a streambuf class that held an instance of another streambuf (through parameterized inheritance) and would ferry calls from the one to the other, tracking data as it was read, but I wasn't...
9
3988
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 main(void) { char buf = {0};
0
5655
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 ChatDispatch.java
1
2327
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 a real-time spectrum analysis program (for ham radio) using ossaudiodev, which seems to be working but with one problem. There is excessive buffering delay between electrical signal input and availability to ossaudiodev.read(). The lower the sample...
1
2372
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 in the right direction. Ok the problem. I am creating a program that ask the user to input a height value, the program will then do a calculation and create a golden ratio width. The type of both the height and the width are double. This is...
1
6487
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 a file called Average which is the name of the source file except the source file has a .py extension. So anyways I was wondering how you can put the Average file onto a webpage. This is a bit of an HTML question but I am using a python program. ...
1
5114
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 following code. in the following code i am using SelectionArea class which extends a canvas on which i am performing drawing operation. i am using image variable in this class for double buffering to reduce flickering and to save the applet's previous...
0
7880
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8255
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8374
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8010
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8242
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6665
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
3903
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2389
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1486
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.