首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > Java Web开发 >

:请教怎么解决JAVA socket通信时的缓冲区的清空有关问题

2012-02-19 
紧急求助:请问如何解决JAVA socket通信时的缓冲区的清空问题我用servlet作的socket连接客户端的访问程序;

紧急求助:请问如何解决JAVA socket通信时的缓冲区的清空问题
我用servlet作的socket连接客户端的访问程序;需要用web页面通过socket收发远程客户端的数据信息;每次连接客户端是通过数据库查询ip及port后,用相应的ip及port进行连接;为什么连上以后向客户端发送信息后再读取客户端返回过来的信息都是一样的?servlet不是多线程的吗?每连接一个socket应该都是新的线程;为什么输入缓冲区里的数据还是一直是同一个客户端的数据;连接新的连接后读取缓冲区的数据还是旧的?缓冲区是共享的吗?请问能不能为每个连接建立独立的缓冲区?谢谢!

下面是我的一部分代码请大家指教:

servlet的
  public   void   doGet(HttpServletRequest   request,   HttpServletResponse   response)   throws
                        ServletException,   IOException   {
                response.setContentType(CONTENT_TYPE);
                PrintWriter   out   =   response.getWriter();
                sta=(StationVO)   request.getAttribute( "setst ");\\得到数据库查询数据
                out.println( " <input   type=hidden   name=id   value=\ " "+sta.getId()+ "\ "/> ");
                out.println( " <input   type=hidden   name=type   value=\ " "+request.getParameter( "type ")+ "\ "/> ");
                String   host=sta.getSIp();\\从bean得到查询的ip
                int   port=sta.getSPort();\\从bean得到查询的port
                if(socketbean.cSocket==null){
                          socketbean.connectionsocket(host,port);\\通过socketbean连接
                  }
                else   if(socketbean.cSocket!=null){
                          try   {
                                  socketbean.searchtw();
                                  if   (socketbean.getDataInputStream()   !=   null)   {
                                          socketbean.read();\\每次读的还是以前的数据,如何解决?
                                  }
                          }   catch   (Exception   e)   {

                    }            

socketbean的代码:

import   java.net.*;
import   java.io.*;
import   java.util.*;
import   java.lang.*;

public   class   SocketBean   {

        public   SocketBean()   {
        }
        private   String   Address;   //服务器地址
        private   int   Port;   //服务器端口
        public   Socket   cSocket   =   null;   //Socket
        public   byte[]   a   =   new   byte[16];
        private   DataOutputStream   dos   =   null;


        private   DataInputStream   dis   =   null;
        boolean   bconnect   =   false;
        private   static   SocketBean   me=new   SocketBean();
        public   static   SocketBean   newInstance()   {
              return   me;
        }

        public   void   setDataOutputStream(DataOutputStream   Dos){
                dos=Dos;
        }
        public   DataOutputStream   getDataOutputStream(){
                return   dos;
        }
        public   void   setDataInputStream(DataInputStream   Dis){
                dis=Dis;
        }
        public   DataInputStream   getDataInputStream(){
                return   dis;
        }

        public   void   setAddress(String   address)   {   //设置服务器地址
                Address   =   address;
        }

        public   void   setPort(int   port)   {   //设置服务器端口
                Port   =   port;
        }

        public   String   getAddress()   {   //获取服务器地址
                return   Address;
        }

        public   int   getPort()   {   //获取服务器端口
                return   Port;
        }


        public     void   connectionsocket(String   HostName,int   Port){

              try   {
                      this.Address=HostName;
                      this.Port=Port;
                      SocketAddress   addr   =   new   InetSocketAddress(Address,   Port);
                      //生成一个socket
                      cSocket=new   Socket(Address,Port);
                      cSocket.setKeepAlive(true);
                      dos   =   new   DataOutputStream(cSocket.getOutputStream());
                      dis   =   new   DataInputStream(cSocket.getInputStream());
                      bconnect   =   true;
                      while   (!cSocket.isConnected())   {


                              System.out.println( "connection   has     not   been   established!... ");
                      }
                      System.out.println( "connection   has   been   established!... ");
              }   catch   (IOException   e)   {
                      System.out.println(e);
              }
      }

        public   void   Close()   {
                try   {
                        if   (cSocket   !=   null)   {
                                cSocket.close();
                        }
                }   catch   (IOException   e)   {
                        System.out.println(e);
                }   finally   {
                        try   {
                                dos.close();
                                dis.close();
                                cSocket.close();
                        }   catch   (IOException   ex)   {
                        }
                }
        }

        public   void   searchtw()   {
                        int[]   b   =   new   int[8];
                        b[0]   =   0xf8;
                        b[1]   =   0xf9;
                        b[2]   =   0x0f;
                        b[3]   =   0xff;
                        b[4]   =   0x24;
                        b[5]   =   0x00;
                        b[6]   =   0xfe;
                        b[7]   =   0xff;
                        try   {


                                        dos.write(b[0]);
                                        dos.write(b[1]);
                                        dos.write(b[2]);
                                        dos.write(b[3]);
                                        dos.write(b[4]);
                                        dos.write(b[5]);
                                        dos.write(b[6]);
                                        dos.write(b[7]);
                                        dos.flush();
                        }
                        catch   (IOException   e)   {
                                e.printStackTrace();
                        }
        }

        public   void   read()   {
                try   {
                                if(dis!=null){dis.readFully(a);
                                        System.out.println(a[5]);
                                        System.out.println(a[6]);
                                        System.out.println(a[7]);
                                        System.out.println(a[8]);
                                        System.out.println(a[9]);
                                        System.out.println(a[10]);
                                        System.out.println(a[11]);
                                        System.out.println(a[12]);
                                }


                                System.out.println( " ");

                }   catch   (Exception   ex)   {
                }
        }
}




[解决办法]
缓冲区好象是共享的,帮你顶下。
[解决办法]
dos.close();
dis.close();

热点排行