hc-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Thomas Förster ...@thofoer.de>
Subject Asynchronous client-server communication
Date Fri, 11 Mar 2005 17:27:26 GMT
Hi!

I need to implement a bidirectional, fully asynchronous client-server
communication. Normally the java.net-API would do fine, but I also have
to deal with firewalls/proxies. So I'm trying to use servlets and 
HttpClient.

There are  many examples of servlet-applet/application communication, but 
due  to the nature
of HTTP everything is a synchronous "client-sends-request 
server-sends-response".

In my application client and server will have to send and receive data 
 from the other side
whenever they want. So I had the idea of creating two connections, one for 
upstream and
one for downstream. These connections will have to be kept alive during 
runtime of the
client session.

That's the theory. In practice I don't get the output-stream 
client->server to work.
Here's a little code I wrote with the java.net-API: In the servlet:

     public void doPost(HttpServletRequest request, HttpServletResponse 
response) throws IOException, ServletException {
         BufferedReader in = new BufferedReader(new 
InputStreamReader(request.getInputStream()));
         while(true) {
             Object l = in.readLine();           // read anything from the 
input stream and print it
             System.out.println("recv-->"+l);
             if (l==null) return;
         }
     }


Client-side (output-stream to server):

         url = new URL( "http://127.0.0.1/url_to_servlet" );
         URLConnection con = url.openConnection();
         con.setDoOutput(true);
         con.setDoInput(true);
         con.setUseCaches(false);
         PrintWriter out   = new PrintWriter(con.getOutputStream());
         out.println("Hello?\n");
         out.flush();

This doesn't work. Nothing is received by the servlet.
Only when I add a

         con.getInputStream();

to the client code (after out.flush()) the servlet receives the string. 
But then I cannot
use the output stream anymore.


Next I tried the HttpClient PostMethod with a RequestMethod:

         HttpClient client = new HttpClient();
         PostMethod httppost = new 
PostMethod("http://127.0.0.1/url_to_servlet");
         httppost.setRequestEntity(new RequestEntity() {
             public void writeRequest(OutputStream out) throws IOException 
{
                 for (int i=0;i<3;i++){
                     // send 3 strings with 3 seconds pause
                     w.println("Hello?\n");
                     w.flush();
                     try {
                         Thread.sleep(3000);
                     } catch (InterruptedException e) {  }
                 }
             }
           [....]
         }
         client.executeMethod(httppost);

The result is similar. The strings are not received at once by the 
servlet. Only when the
writeRequest-method returns the three strings arrive on the server-side.

What am I doing wrong?
Is that asynchronous communication possible at all?

Thank you for reading my post.
Any help appreciated.

Thomas

---------------------------------------------------------------------
To unsubscribe, e-mail: httpclient-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: httpclient-dev-help@jakarta.apache.org


Mime
View raw message