your test client is wrong,I've pasted in the correct one

only three changes
1. set the timeout so that the socket stays alive
2. keep reading data so that the socket stays alive
3. 0crlfcrlf as the last chunk

works as expected. to make a workable client, it should read until it gets the last-chunk, not read forever like I am doing. I just didn't see the need to put in http parsing in the code to demonstrate it.

also, I applied the timeout patch that I wrote about earlier,so I am setting the timeout in the comet servlet, without the patch its using the connectionTimeout value

package test;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.NoRouteToHostException;
import java.net.Socket;
import java.net.URL;

public class TestClient {

   private static final String ENCODING = "ISO-8859-1";

   private static final String DELIMITER = "\r\n";

   private URL url;

   private InputStream inputStream;

   private OutputStream outputStream;

   private Socket socket;

   public static void main(String[] args) throws Exception {
       TestClient test = new TestClient();
       test.test();
   }

   private void test() throws Exception {
       url = new URL("http://localhost:8080/comet";);
       initConnection();
       sendHeaders();
       send("comet test");
       // uncomment sleep to get END event instead of read error
//        try {
//            Thread.sleep(50);
//        } catch (InterruptedException ie) {
//            // do nothing
//        }
       sendLastChunk();
   // block on read to keep app alive - server never sends response
       byte[] data = new byte[8192];
       int result = inputStream.read(data);

       while (result>0) {
           String s = new String(data,0,result);
           System.out.println("Received data:\n"+s);
           result = inputStream.read(data);
       }
       {
           System.out.println("Received EOF");
       }
   }

   private void initConnection() throws IOException {
       int port = url.getPort();
       port = (port < 0) ? url.getDefaultPort() : port;
       try {
           socket = new Socket(url.getHost(), port);
           socket.setSoTimeout(Integer.MAX_VALUE);
           socket.setKeepAlive(true);
           inputStream = socket.getInputStream();
           outputStream = socket.getOutputStream();
       } catch (NoRouteToHostException nrthe) {
           System.out.println("host: " + url.getHost());
           nrthe.printStackTrace();
       }
   }

   private void sendHeaders() throws IOException {
       String path = url.getPath();
       StringBuffer outputBuffer = new StringBuffer();
       outputBuffer.append("POST " + path + " HTTP/1.1" + DELIMITER);
       outputBuffer.append("Host: " + url.getHost() + DELIMITER);
       outputBuffer.append("User-Agent: CometTest" + DELIMITER);
       outputBuffer.append("Connection: keep-alive" + DELIMITER);
       outputBuffer.append("Content-Type: text/plain" + DELIMITER);
       outputBuffer.append("Transfer-Encoding: chunked" + DELIMITER);
       outputBuffer.append(DELIMITER);
       byte[] outputBytes = outputBuffer.toString().getBytes(ENCODING);
       outputStream.write(outputBytes);
       outputStream.flush();
   }

   private void send(String chunkData) throws IOException {
       byte[] chunkBytes = chunkData.getBytes(ENCODING);
       String hexChunkLength = Integer.toHexString(chunkBytes.length);
       StringBuffer outputBuffer = new StringBuffer();
       outputBuffer.append(hexChunkLength);
       outputBuffer.append(DELIMITER);
       outputBuffer.append(chunkData);
       outputBuffer.append(DELIMITER);
       byte[] outputBytes = outputBuffer.toString().getBytes(ENCODING);
       outputStream.write(outputBytes);
       outputStream.flush();
   }

   private void sendLastChunk() throws IOException {
byte[] outputBytes = new String("0" + DELIMITER+DELIMITER).getBytes(ENCODING);
       outputStream.write(outputBytes);
       outputStream.flush();
   }
}

Peter Warren wrote:
Just to make sure that I wasn't crazy, I did some more tests,
including using your cometgui.jar.  My results still show both read
errors and END events.  Is it possible that it's a platform issue?
I'm running Windows XP Pro SP2.  As I mentioned before I'm using the
tomcat 6.0.x trunk as of 21-01-2008.  Also, I re-started tomcat in
between all tests to make sure there weren't stray bytes hanging
around somewhere.  My server code is the same as shown previously in
this thread.

Peter

Here are my results:

1) Running LastChunkTest (code below) with last chunk of 0crlfcrlf,
server shows:

event: BEGIN, subtype: null
event: READ, subtype: null
Read 10 bytes: comet test for session: 627378C9DEE2817A93EBAC190DE47599
read error

2) Running LastChunkTest with last chunk of 0crlfcrlf and a 50 ms
sleep before sending last chunk, server shows:

event: BEGIN, subtype: null
event: READ, subtype: null
Read 10 bytes: comet test for session: A00EFFC9A9FE8CBF1833D204EF00667C
event: END, subtype: null

3) Running LastChunkTest with a last chunk of 0crlf, server shows:

event: BEGIN, subtype: null
event: READ, subtype: null
Read 10 bytes: comet test for session: DD41C198E3D5CD6E7D73FC0D7B37E86F

4) Running cometgui.jar with 0crlfcrlf:

client sends (without the dashes):


---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to