紧急求助:请问如何解决JAVA socket通信时的缓冲区的清空问题

irro 2007-04-16 10:26:11
我用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) {
}
}
}


...全文
755 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
irro 2007-04-18
  • 打赏
  • 举报
回复
up一下
irro 2007-04-17
  • 打赏
  • 举报
回复
试过是没有用的;看过另外一个讨论http://community.csdn.net/Expert/topic/5066/5066457.xml?temp=.4075281
引用其中一个回答,我觉得还是有道理;如果是这样的话要这么实现;我试过用线程绑定socket也不行;不知道nio可不可以
(InputStream不是你用了getInputStream()方法才出现的,在socket创建之后它就存在了!
而且InputStream只负责从windows的接收缓冲区里取数据,它本身并不负责接收任何数据,接收数据的低层的系统API,所以不管你用没用getInputStream()方法,那些数据就已经存在接收缓冲区里了,必须用InputStream的read方法读取后才会被清除。

所以你说在调用getInputStream()之前就清空接收缓冲区我认为是不可能的,如果那样做的话,岂不是连你想要的数据都清除了??!!)


adverse 2007-04-16
  • 打赏
  • 举报
回复
缓冲区好象是共享的,帮你顶下。
ruanjiantaotao 2007-04-16
  • 打赏
  • 举报
回复
dos.close();
dis.close();

81,092

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧