Return-Path: Delivered-To: apmail-jakarta-httpclient-dev-archive@www.apache.org Received: (qmail 78497 invoked from network); 15 Mar 2005 17:03:45 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (209.237.227.199) by minotaur-2.apache.org with SMTP; 15 Mar 2005 17:03:45 -0000 Received: (qmail 69539 invoked by uid 500); 15 Mar 2005 17:03:45 -0000 Delivered-To: apmail-jakarta-httpclient-dev-archive@jakarta.apache.org Received: (qmail 69274 invoked by uid 500); 15 Mar 2005 17:03:43 -0000 Mailing-List: contact httpclient-dev-help@jakarta.apache.org; run by ezmlm Precedence: bulk List-Unsubscribe: List-Subscribe: List-Help: List-Post: List-Id: "HttpClient Project" Reply-To: "HttpClient Project" Delivered-To: mailing list httpclient-dev@jakarta.apache.org Received: (qmail 69261 invoked by uid 99); 15 Mar 2005 17:03:43 -0000 X-ASF-Spam-Status: No, hits=0.1 required=10.0 tests=NORMAL_HTTP_TO_IP X-Spam-Check-By: apache.org Received-SPF: pass (hermes.apache.org: local policy) Received: from server57.greatnet.de (HELO server57.greatnet.de) (83.133.97.174) by apache.org (qpsmtpd/0.28) with ESMTP; Tue, 15 Mar 2005 09:03:41 -0800 Received: from hermes (p54861505.dip0.t-ipconnect.de [84.134.21.5]) by server57.greatnet.de (server57.greatnet.de) with ESMTP id 7BE2741E002 for ; Tue, 15 Mar 2005 18:03:33 +0100 (CET) To: HttpClient Project Subject: Re: Asynchronous client-server communication References: <78f7873e05031207249f95647@mail.gmail.com> Message-ID: From: =?iso-8859-15?Q?Thomas_F=F6rster?= Content-Type: text/plain; format=flowed; charset=iso-8859-15 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Date: Tue, 15 Mar 2005 18:03:28 +0100 In-Reply-To: <78f7873e05031207249f95647@mail.gmail.com> User-Agent: Opera7.23/Win32 M2 build 3227 X-Virus-Checked: Checked X-Spam-Rating: minotaur-2.apache.org 1.6.2 0/1000/N Thank you for the answer, it was very helpful. Now I subclassed PostMethod and overrode writeRequestBody() where I use the connection's output-stream. This works pretty well (but admittedly it looks pretty ugly): HttpClient client = new HttpClient(); PostMethod httppost = new PostMethod(BASE_URL +"?mode=in") { protected boolean writeRequestBody(HttpState state, HttpConnection conn) throws IOException, HttpException { PrintWriter out = new PrintWriter(conn.getRequestOutputStream()); for (int i=0;i<3;i++){ System.out.println("->send "+i); out.println("test "+i); out.flush(); try { Thread.sleep(3000); } catch (InterruptedException e) {} } System.out.println("EXIT"); conn.getRequestOutputStream().flush(); conn.getRequestOutputStream().close(); return true; } }; httppost.setContentChunked(false); httppost.setRequestEntity(new RequestEntity() { public boolean isRepeatable() { return false; } public void writeRequest(OutputStream out) throws IOException { // not called } public long getContentLength() { return 20000000; } public String getContentType() { return "text/text"; }} ); client.executeMethod(httppost); The strings sent by the client arrive at the server instantly. The main thing I have problems with is the content length. I don't know what to choose here, since the connection is used during the whole session. So I used a very large number (hoping that a client will never send more than 20000000 bytes). I also tried it with "content-chunked", but that's causing problems, as you predicted. Thomas On Sat, 12 Mar 2005 10:24:17 -0500, Michael Becke wrote: > Hi Thomas, > > This may be difficult to make work since as you mention HTTP is not > meant to be used this way. Have a look at EntityEnclosingMethod for > how RequestEntities are used > > for some ideas. First off I think using chunked encoding will be a > problem, so you'll want to not use that. Also, be sure to flush the > output stream when you're done writing. > > Mike > > > On Fri, 11 Mar 2005 18:27:26 +0100, Thomas F�rster wrote: >> 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 >> >> > > --------------------------------------------------------------------- > To unsubscribe, e-mail: httpclient-dev-unsubscribe@jakarta.apache.org > For additional commands, e-mail: httpclient-dev-help@jakarta.apache.org > > -- Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/ --------------------------------------------------------------------- To unsubscribe, e-mail: httpclient-dev-unsubscribe@jakarta.apache.org For additional commands, e-mail: httpclient-dev-help@jakarta.apache.org