How do you send multiple requests to the same comet servlet? Sending multiple chunks of a single request is fine. My problem occurs after the client ends the chunked transaction by sending "0CRLFCRLF" to the server. The comet servlet correctly registers the END event.
But then the client subsequently tries to initiate a new chunked request by sending new http headers and a new chunk. The comet servlet receives a BEGIN event, immediately followed by an END event. A READ event is never generated for the new chunk. No ERROR events are registered either. Sequence: 1st request BEGIN READ END 2nd request BEGIN END - why an END event and not a READ? Below are my test client and test comet servlet. Any thoughts on what I'm doing wrong? (I apologize in advance if this is a lack of understanding of http on my part. I have looked at the specs and tried to follow the rules but am still running into problems.) Thanks, Peter ----- <test servlet> import java.io.IOException; import java.io.InputStream; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.catalina.CometEvent; import org.apache.catalina.CometProcessor; public class CometTestServlet extends HttpServlet implements CometProcessor { private static final long serialVersionUID = 5472498184127924791L; public void event(CometEvent cometEvent) throws IOException, ServletException { HttpServletRequest request = cometEvent.getHttpServletRequest(); HttpServletResponse response = cometEvent.getHttpServletResponse(); // don't want timeout events cometEvent.setTimeout(1000000); if (cometEvent.getEventType() == CometEvent.EventType.BEGIN) { log("Begin for session: " + request.getSession(true).getId()); } else if (cometEvent.getEventType() == CometEvent.EventType.ERROR) { log("Error for session: " + request.getSession(true).getId() + ", " + cometEvent.getEventSubType()); cometEvent.close(); } else if (cometEvent.getEventType() == CometEvent.EventType.END) { log("End for session: " + request.getSession(true).getId()); cometEvent.close(); } else if (cometEvent.getEventType() == CometEvent.EventType.READ) { log("Read for session: " + request.getSession(true).getId()); respond(request, response); } } private void respond(HttpServletRequest request, HttpServletResponse response) throws IOException { String clientMessage = read(request); if (clientMessage != null && clientMessage.length() > 0) { response.getWriter().print(clientMessage); response.getWriter().flush(); } } private String read(HttpServletRequest request) throws IOException { InputStream is = request.getInputStream(); StringBuffer inputBuffer = new StringBuffer(); byte[] buf = new byte[512]; while (is.available() > 0) { int n = is.read(buf); // can throw an IOException if (n > 0) { inputBuffer.append(new String(buf, 0, n)); log("Read " + n + " bytes: " + new String(buf, 0, n) + " for session: " + request.getSession(true).getId()); } else if (n < 0) { log("comet read error"); } } return inputBuffer.toString(); } } </test servlet> ----- <test client> import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.net.Socket; public class CometTestClient1 { public static final String ENCODING = "ISO-8859-1"; private Socket socket; private OutputStream out; public static void main(String[] args) throws Exception { CometTestClient1 test = new CometTestClient1(); test.test(); } public CometTestClient1() throws IOException { initConnection(); } private void initConnection() throws IOException { socket = new Socket("127.0.0.1", 80); socket.setKeepAlive(true); out = socket.getOutputStream(); sendHeaders(); } private void sendHeaders() throws IOException { println("GET /CometTest HTTP/1.1"); println("Host: 127.0.0.1"); println("User-Agent: test"); println("Transfer-Encoding: chunked"); println("Connection: keep-alive"); } private void test() throws IOException { sendChunkedMessage(); try { Thread.sleep(60000); } catch (InterruptedException ie) { ie.printStackTrace(); } // doesn't seem to matter if I create a new socket connection or not //initConnection(); sendHeaders(); sendChunkedMessage(); } private void sendChunkedMessage() throws IOException { println(); println("10"); print("test data test 1"); out.flush(); String line = read(); System.out.println(line); println(); println("0"); println(); out.flush(); line = read(); System.out.println(line); } private void print(String chars) throws IOException { out.write(chars.getBytes(ENCODING)); } private void println(String chars) throws IOException { out.write((chars + "\r\n").getBytes(ENCODING)); } private void println() throws IOException { out.write(("\r\n").getBytes(ENCODING)); } private String read() throws IOException { InputStream in = socket.getInputStream(); StringBuffer inputBuffer = new StringBuffer(); byte[] buf = new byte[512]; do { int n = in.read(buf); // can throw an IOException if (n > 0) { inputBuffer.append(new String(buf, 0, n)); } else if (n < 0) { return ("read error"); } } while (in.available() > 0); return inputBuffer.toString(); } } </test client> ----- <test results> HTTP/1.1 200 OK Server: Apache-Coyote/1.1 Set-Cookie: JSESSIONID=775B71AFC0066AA45224A4F00309D101; Path=/ Transfer-Encoding: chunked Date: Tue, 14 Aug 2007 20:00:49 GMT 10 test data test 1 0 HTTP/1.1 200 OK Server: Apache-Coyote/1.1 Set-Cookie: JSESSIONID=BF0C8B7A0D27CDCA6A285F9C4C598613; Path=/ Content-Length: 0 Date: Tue, 14 Aug 2007 20:00:49 GMT read error </test results> ----- connector configuration for tomcat 6.0.14 on windows xp: <Connector port="80" protocol="org.apache.coyote.http11.Http11NioProtocol" maxThreads="150" connectionTimeout="120000" keepAliveTimeout="300000" maxKeepAliveRequests="-1" socket.soKeepAlive="true" acceptorThreadCount="2" redirectPort="8443" /> ----- Catalina log registering begin, read, and end events for initial request, followed by begin and end events for subsequent request: Aug 14, 2007 1:02:37 PM org.apache.catalina.core.ApplicationContext log INFO: CometTest: Begin for session: 910EA1A41AB07465D61585930564AFD0 Aug 14, 2007 1:02:37 PM org.apache.catalina.core.ApplicationContext log INFO: CometTest: Read for session: 910EA1A41AB07465D61585930564AFD0 Aug 14, 2007 1:02:37 PM org.apache.catalina.core.ApplicationContext log INFO: CometTest: End for session: 910EA1A41AB07465D61585930564AFD0 Aug 14, 2007 1:02:37 PM org.apache.catalina.core.ApplicationContext log INFO: CometTest: Begin for session: 22A5A802D4A201A024939BE724A1F530 Aug 14, 2007 1:02:37 PM org.apache.catalina.core.ApplicationContext log INFO: CometTest: End for session: 22A5A802D4A201A024939BE724A1F530