现在有个项目要用到http长连接,不能用socket,客户端代码: HttpURLConnection httpConn = (HttpURLConnection) httpurl
.openConnection();
httpConn.setDoOutput(true);
httpConn.setDoInput(true);

httpConn.setRequestProperty("Connection", "keep-alive");
PrintWriter out = new PrintWriter(httpConn.getOutputStream());
out.print("**39000000101356433677500");
out.flush(); InputStream in = httpConn.getInputStream();
/**********************************
这里服务器端doPost方法响应,读出了*39000000101356433677500
***********************************/

Runnable myThreadUse=new testTrackeListen(new DataInputStream(in));
        Thread myThread=new Thread(myThreadUse);
        myThread.start(); /**********************************
服务器端读了一次**39000000101356433677500 ,下面的**39全部收不到
***********************************/

out.print("**39000000101356433677500");
out.flush(); out.print("**39000000101356433677500");
out.flush();
out.print("**39000000101356433677500");
out.flush();
out.print("**39000000101356433677500");
out.flush();
out.print("**39000000101356433677500");
out.flush();服务器代码 : public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
out = response.getOutputStream();
// in = request.getInputStream();
Reader in = request.getReader();

this.sendMessage("服务器发送");
int bytesread = 0;
char[] rawtext = new char[10000];
bytesread = in.read(rawtext);
if (bytesread <= 0) {
// continue;
}
System.out.println(bytesread);
/**********************************
只读了一次**39000000101356433677500 ,InputStream in = httpConn.getInputStream();之前发送的那次,下面的**39全部收不到,bytesread全部是-1;
***********************************/
this.sendMessage("服务器发送");

bytesread = in.read(rawtext); System.out.println(bytesread);

bytesread = in.read(rawtext); System.out.println(bytesread);
}
我是菜鸟,请问怎么样才能让Servlet 持续的接收客户端发送的内容?  这个问题困扰我很久了 ,任务也快到了 ,急,哪位英雄解答一下,跪谢(this.sendMessage("服务器发送");两次发送的消息,testTrackeListen都能收到)

解决方案 »

  1.   

    out.print("**39000000101356433677500");
    out.print("**39000000101356433677500\r\n");or out.println("**39000000101356433677500");
      

  2.   

    我没有用readline所以可以不加\r\n 
      

  3.   

    这么有前途的贴怎么没人进  实在不行我只能看下tomcate6 的comet了,那位天使大哥大姐来解答一下了 
      

  4.   

    一下子也了解不了你的代码,我们写一个webcmd或许对你有点帮助。
    我们在页面里使用ajax向后台发一次请求,维持长连接,一旦后台有数据输出就获取数据,然后返回,并结束请求(否则在ie下无法获取数据),然后显示数据,同时再发请求。而后台代码是,如果过了3秒还没数据产生,那么也返回。如此循环往复。action代码
    http://code.google.com/p/simplejee/source/browse/trunk/simplejee/src/com/yuqiaotech/simplejee/struts2/action/CmdAction.java
    页面代码
    http://code.google.com/p/simplejee/source/browse/trunk/simplejee/WebContent/cmd_window.jsp
      

  5.   

    代码我看了  
    我的代码中客户端的请求能收到 dopost中能用个while持续不断的或者是延迟的向客户段推数据,
    但是我想用
    Reader in = request.getReader(); 这个来持续不断的接收客户端来的数据,就象socket一样,但是在Servlet中不行,是不是根本就不支持这样做?
      

  6.   

    似乎做不到,因为javascript没这个支持。
    客户端只能是一次请求,一次回应。发送请求时首先要确定好发送的内容。
    不知flex是否有相关内容。
    再或者就是变通的实现了。在后台起个接收的线程,和你的主程序公用一个存储。
      

  7.   

    客户端不是浏览器  是手机   手机有两种网络连接方式  cmnet 就是socket  cmwap 就是http协议  之前做了socket长连接  但cmwap包月更省钱  所以我们就要用servlet来接受手机的请求 然后问题随之而来 在线等 呵呵 
      

  8.   

    标准的servlet api应该本身就是不允许这样的异步长请求(保持连接,不断接收和返回数据)要实现这样的功能。tomcat参考
    http://tomcat.apache.org/tomcat-6.0-doc/aio.htmlweblogic 可以到网上搜下 FutureResponseServlet FutureRequestServlet
      

  9.   

    我KAO,竟然中计了。原来是又读又写,竟然采用HTTP协议。
      

  10.   


    public void event(CometEvent event) throws IOException, ServletException {
    // protocol="org.apache.coyote.http11.Http11NioProtocol"

    HttpServletRequest request = event.getHttpServletRequest();
    HttpServletResponse response = event.getHttpServletResponse();
                            if (event.getEventType() == CometEvent.EventType.READ) { 
    PrintWriter out = response.getWriter();
    InputStream is = request.getInputStream();
    System.out.println("开始");
    int a = 0;
    while (true) {
    byte[] buf = new byte[512];
    do {
    System.out.println("正在读"); 
    int n = is.read(buf);
    System.out.println("读完a  "+n);
                                              } while (is.available() > 0); try {
    Thread.sleep(5000);
    } catch (InterruptedException e) {
    e.printStackTrace(); 
    } String msg = "a" + a;
    try {
    out.write(msg);
    out.flush();
    } catch (Exception e) {
    e.printStackTrace();
    }
    a++;
    }
    }今天用了tomcat6的comet ,代码如上 ,客户端依然是用HttpURLConnection代码没变,
    int n = is.read(buf);这句读出的还是-1,和普通servlet一模一样,那位高手近来看一看
      

  11.   

    https          回复内容太短了! 
      

  12.   

    我感觉tomcat6的comet只是让response便于管理  这种长连接只能适应特殊的情况 ,对于要根据用户请求改变下发数据的情况还是要用请求一次响应一次这样老方法,这根本不是什么新技术,只能叫模式,再没高手来就结贴了
      

  13.   

    请看这个图片。
    所谓的长连接并不是你想像的:客户端和服务端一直连接,客户端发一个消息,服务端回一个消息,客户端又可以再发一个消息,如此循环而是: 客户端请求服务端,发送一个消息,服务端这时候可以无限等待,也可以慢慢的送几个消息到客户端(stream),但是注意,这时候客户端是不能再请求消息过去,等到服务器认为结束了,那么终止返回数据,客户端断开连接。如此循环,每次客户端要发消息一定要再建立连接。
    所谓的长连接只是表示服务端可以暂停返回数据,让客户端一直等待。不知道我表达的意思你明白没? 看你的代码貌似以为comet能实现socket这样的数据传输。
      

  14.   

    完全明白  但是不试不知道  我就是以为comet能实现  行了  结贴 给分
      

  15.   

    The JavaScript client sends two messages to the WebSocket servlet: "hello", and "server". When the servlet receives "hello", it returns "world". When it receives "server" it returns "Resin".When the client receives the response, it adds the received text to the "Received: " area.说明WebSocket支持多次往返交互的.