Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / January 2008

Tip: Looking for answers? Try searching our database.

SERVLET: Http Connection doesn't send data

Thread view: 
jmackquack@gmail.com - 05 Jan 2008 13:39 GMT
I'm trying to write an application client/server with http protocol,
in JAVA. I need:

- client connects to the server,and after that,
- server continuously sends data to the client (as soon as they can be
obtained by an other server-side resource !! )

Client-side code is:

            HttpURLConnectionis = httpConnection.getInputStream();

            ObjectInputStream ois = new ObjectInputStream(is);
            String toWrite=null;
            do {
                toWrite=(String)ois.readObject();
            }
            while(Thread.currentThread().isAlive());
            is.close();

Server-side code (part of method doGet() of a servlet)  is:

            ObjectOutputStream os= new ObjectOutputStream
(response.getOutputStream());
            Signal sig; // Signal is a Java Class containing String messages

            int i=0;
            while (i<10) {
                sig = obs.getSignal(); //  "obs" is a LinkedBlockingQueue!!!!
                os.writeObject(new String(sig.getValue())); // sig.getValue()
returns String
            }

The problem is that data sent from server arrives to client ALL at the
end of the while-cicle, and not one by one!

Istead, by using a code like this:

            ObjectOutputStream os= new ObjectOutputStream
(response.getOutputStream());
            int j=0;
            while(true){
                os.writeObject(new String("\n SERVER: count"+j));
                j++;
            }

...data arrives one by one!

I made lots of tries, variously modifying the code..and I noticed that
the problem is in using blocking methods
(in fact, the behaviour is same if forcing the current Thread to sleep
- eg Thread.sleep(1000) -).

Where's the problem??
Godofredo - 06 Jan 2008 15:01 GMT
On Jan 5, 8:39 am, jmackqu...@gmail.com wrote:
> I'm trying to write an application client/server with http protocol,
> in JAVA. I need:
[quoted text clipped - 49 lines]
>
> Where's the problem??

Try os.flush();
Best wishes
Alex
EJP - 07 Jan 2008 00:03 GMT
>             do {
>                 toWrite=(String)ois.readObject();
>             }
>             while(Thread.currentThread().isAlive());

That termination condition is crazy. The loop will nominally run
forever. It should terminate when EOFException is caught.

>             is.close();

This line of code is not reachable. If the current thread isn't alive
how can it execute this line of code?

> The problem is that data sent from server arrives to client ALL at the
> end of the while-cicle, and not one by one!

That's how HTTPResponse works unless you use chunked streaming mode,
because it has to assemble the entire response and set the
content-length header before sending the content.

> (in fact, the behaviour is same if forcing the current Thread to sleep
> - eg Thread.sleep(1000) -).

There's no reason why that would make any difference.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.