File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Applets and the fly likes How to make a repetitive URLConnection ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Applets
Bookmark "How to make a repetitive URLConnection ?" Watch "How to make a repetitive URLConnection ?" New topic
Author

How to make a repetitive URLConnection ?

Wolfgang Tintemann
Ranch Hand

Joined: Sep 11, 2010
Posts: 65

In an applet I want to make an endless repetitive URLConnection to a servlet.
This means I plan to use a while(true) loop and open the connection within it.

Now my knowledge/understanding ends.

Can I establish the connection outside of the loop once and make my
permanent reads from the servlet within the loop - without ever closing the connection ?

Does it somehow harm the net ( traffic ) when I make the URLConnection within
the infinite loop ?
In an example of a chat program which I studied the authors made the URLConnection
within an infinite loop - this even within a separate thread.

Time isn't Money, Money is Time.
The man who works looses his time ( spanish proverb )
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

No, the way HTTP works is that you must make one connection per request.

(This is assuming that you haven't used some technique like Comet (programming) in which you have a permanent connection, which you most likely haven't.)
Wolfgang Tintemann
Ranch Hand

Joined: Sep 11, 2010
Posts: 65

What i am still unsure about is :
does the while loop be interrupted when the read
command to the servlet is called ?

Thanks for your info, Paul.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

Wolfgang Tintemann wrote:does the while loop be interrupted when the read command to the servlet is called ?


I don't think so. Not for any of the usual meanings of the word "interrupted" anyway. First the statement before the "read command" is executed. Then the connection to the server is made. Then (assuming no exceptions were thrown) the statement after that is executed. Is there some reason why you would describe the step in the middle as "interrupting" the while loop? Because I find hard to guess what was the reason you asked the question that way.
Wolfgang Tintemann
Ranch Hand

Joined: Sep 11, 2010
Posts: 65

Hm,
what i want to know is whether the while loop
stops when the read is executed and waits
for the response from the servlet ?
Or : are there done dozens, hundreds or millions
of URLConnections in the loop ?
I wonder how http deals with that.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

Well, of course. Just like it stops and waits while you add one to a variable if that's what your code is supposed to do. I don't see why this is such a mystery: it's just Java code. First one statement is executed and then the next statement is executed.

Perhaps you could post some code which illustrates this extremely strange question?
Wolfgang Tintemann
Ranch Hand

Joined: Sep 11, 2010
Posts: 65

Here I have some code from a chat program example.

A snippet from the applet :



Here is the infinite loop i am puzzled about.
My question is about line 14 : here the readLine() reads from the servlet.
DOES THE PROGRAM WAIT HERE FOR THE ANSWER FROM THE SERVLET ???
Or is perhaps some exception thrown and then the loop goes on over and over again ?

Here are the methods needed in the above snippet :

Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

Well, yes. Either line 14 ends normally (at which time it will have done what the documentation says the method does) or it throws an exception. But again, this is true of all Java code.

You seem to think the fact that the "readLine" method is accessing data from somewhere else makes a difference. It doesn't.

Or perhaps you're confused about what the readLine method does? It reads a line (terminated by some line-ending character or characters) from the input stream. So your code (exceptions aside) connects to the servlet and reads the first line of whatever it returns as its response. And it does that repeatedly.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to make a repetitive URLConnection ?