Return-Path: Delivered-To: apmail-tomcat-dev-archive@www.apache.org Received: (qmail 96975 invoked from network); 2 Aug 2006 21:46:19 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (209.237.227.199) by minotaur.apache.org with SMTP; 2 Aug 2006 21:46:19 -0000 Received: (qmail 70855 invoked by uid 500); 2 Aug 2006 21:46:14 -0000 Delivered-To: apmail-tomcat-dev-archive@tomcat.apache.org Received: (qmail 70795 invoked by uid 500); 2 Aug 2006 21:46:13 -0000 Mailing-List: contact dev-help@tomcat.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: "Tomcat Developers List" Delivered-To: mailing list dev@tomcat.apache.org Received: (qmail 70784 invoked by uid 99); 2 Aug 2006 21:46:13 -0000 Received: from asf.osuosl.org (HELO asf.osuosl.org) (140.211.166.49) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 02 Aug 2006 14:46:13 -0700 X-ASF-Spam-Status: No, hits=1.1 required=10.0 tests=DNS_FROM_RFC_ABUSE,HTML_00_10,HTML_MESSAGE,SPF_PASS X-Spam-Check-By: apache.org Received-SPF: pass (asf.osuosl.org: domain of san.tang@gmail.com designates 64.233.162.205 as permitted sender) Received: from [64.233.162.205] (HELO nz-out-0102.google.com) (64.233.162.205) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 02 Aug 2006 14:46:12 -0700 Received: by nz-out-0102.google.com with SMTP id i11so606990nzh for ; Wed, 02 Aug 2006 14:45:52 -0700 (PDT) DomainKey-Signature: a=rsa-sha1; q=dns; c=nofws; s=beta; d=gmail.com; h=received:message-id:date:from:to:subject:mime-version:content-type; b=CZzW1yixCfHxWY2auytmW9Cz4CFh1+hc+0Wu8tBNdXKHAcHU48R0yMFYu53GMzTIIM6fGrK92AqPoMi17A5tRmfSAmOL40UUaXys508T8hG4cMFwOqlhIh3sXK6aRkZj5wWDLGVkksM6UF+t+oJG0NSgUZKkOnUCYlW7F67EpHo= Received: by 10.65.115.18 with SMTP id s18mr37343qbm; Wed, 02 Aug 2006 14:45:51 -0700 (PDT) Received: by 10.64.181.8 with HTTP; Wed, 2 Aug 2006 14:45:21 -0700 (PDT) Message-ID: <97b49b250608021445s50781364m4618557066a30a40@mail.gmail.com> Date: Wed, 2 Aug 2006 17:45:21 -0400 From: "Hung Tang" To: dev@tomcat.apache.org Subject: persistent connections: understanding socket programming in tomcat help MIME-Version: 1.0 Content-Type: multipart/alternative; boundary="----=_Part_40820_20621821.1154555121082" X-Virus-Checked: Checked by ClamAV on apache.org X-Spam-Rating: minotaur.apache.org 1.6.2 0/1000/N ------=_Part_40820_20621821.1154555121082 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Content-Disposition: inline Hi, My requirement is to write a server that is capable of holding long conversations with the client based on socket programming for Java. I'm using ServerSocket to create TCP sockets. The problem I am running into is that I am not able to do [read, write, read] sequence of operations reliably on my socket. Here is my [read, write, read] code: 1: int numBytesRead = socket.getInputStream().read(buffer); 2: System.out.println(numBytesRead); 3: socket.getOutputStream().write(new byte[] { 'a', 'b', 'c' }); 4: socket.getOutputStream().flush(); 5: numBytesRead = socket.getInputStream().read(buffer); 6: System.out.println(numBytesRead); On line 5 in this code, the read operation is NOT blocking and will return -1. After some testing, I concluded that after flushing output [line 4], the input stream will not block anymore. However, if I comment out line 4, the read operation will block again, and will continue to block on subsequent attempts for read before any output flush. What I want is to implement persistent connections which enable me to read client's input, respond, and repeat without closing the socket. I believe this possible with Tomcat but I can't figure out how Http11Processor implements it. Can anyone give me any ideas as to WHY this is the behaviour to be expected in my code? Why is that a output flush causes my input stream to behave unexpectedly? And how do I correctly implement read, write, read for support persistent connections? Surely this can be done right? I only hope so. -- Hung Tang ------=_Part_40820_20621821.1154555121082--