就是那个readMsg的方法  每次read后数据依然存在,我的socket一次连接要多次读写,怎么让read后读过得数据就不存在了,我现在勉强用skip略过前面的数据勉强可以让程序正常,但还是想有什么办法能让数据度过后就没了。大侠帮忙啊  明天交差package com.pbsage.socket;import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;import com.pbsage.util.PropertyUtils;/**
 * socket核心类
 * 
 * @author gzb
 */
public class SocketClient {
private Socket socket = null;
private DataInputStream in; // 输入流
private DataOutputStream out; // 输出流 /**
 * 打开socket通道
 * 
 * 
 * @param serverIP
 * @param serverPort
 * @throws IOException
 * @throws UnknownHostException
 * @throws NumberFormatException
 */
public SocketClient() {
try {
socket = new Socket(PropertyUtils.ADDRESS, Integer
.parseInt(PropertyUtils.PORT));
socket.setSoTimeout(1000 * 10);

in = new DataInputStream(socket.getInputStream());
out = new DataOutputStream(socket.getOutputStream());
} catch (Exception e) {
e.printStackTrace();
}
}
/**
 * 发送数据包(拼装信息头部)
 * 
 * @param msg
 *            报文体
 * @return
 */
public synchronized byte[] sendMsg(byte[] type, byte[] msg) {
byte[] res = null;
try {
byte[] headbytes = new byte[2];
headbytes[0] = 0x1f;
headbytes[1] = 0x1f;
byte[] msgLength = int2ByteArray(msg.length);
byte[] data = new byte[7 + msg.length];
System.arraycopy(headbytes, 0, data, 0, 2);
System.arraycopy(type, 0, data, 2, 1);
System.arraycopy(msgLength, 0, data, 3, 4);
System.arraycopy(msg, 0, data, 7, msg.length);
out.write(data);
res = readAMsg(in);
} catch (Exception e) {
res = "9999".getBytes();
e.printStackTrace();
} finally {
try {
// in.close();
// out.close();
// if (socket != null) {
// socket.close();
// }
} catch (Exception e) {
e.printStackTrace();
}
} return res;
}

public synchronized void closeConnect(){
try {
in.close();
out.close();
if (socket != null) {
socket.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
private static long index = 0;
public synchronized byte[] readAMsg(DataInputStream in) {
try {
byte[] head = new byte[7];
in.skip(index);
in.read(head);
// 取得数据包的长度
byte[] num = new byte[4];
System.arraycopy(head, 3, num, 0, 4);
int length = byteArray2Int(num); byte[] msg = new byte[length];
in.read(msg);
index = length + 7;
return msg;
} catch (Exception e) {
e.printStackTrace();
System.out.println("接收数据异常.");
return ("9999").getBytes();
}
} /**
 * 将字节数组合并
 * 
 * @param b1
 * @param b2
 * @return
 */
public static byte[] arrayAdd(byte[] b1, byte[] b2) {
byte[] b3 = new byte[b1.length + b2.length];
System.arraycopy(b1, 0, b3, 0, b1.length);
System.arraycopy(b2, 0, b3, b1.length, b2.length);
return b3;
} /**
 * 字节数组的低位是整型的高字节位
 * 
 * @param target
 * @return
 */
public static byte[] int2ByteArray(int target) {
byte[] array = new byte[4];
for (int i = 0; i < 4; i++) {
int offSet = array.length - i - 1;
array[i] = (byte) (target >> 8 * offSet & 0xFF);
}
return array;
} /**
 * 字节数组的低位是整型的高字节位
 * 
 * @param array
 * @return
 */
public static int byteArray2Int(byte[] array) {
int result = 0;
byte loop; for (int i = 0; i < 4; i++) {
loop = array[i];
int offSet = array.length - i - 1;
result += (loop & 0xFF) << (8 * offSet); }
return result;
} public static void main(String[] args) throws Exception {
String xml = "<?xml version=\"1.0\" encoding=\"utf-8\" ?><root><common><platform_id>JA</platform_id><type>id_validate</type></common><id_validate operation=\"request\"></id_validate></root>";
byte[] msg = xml.getBytes("UTF-8"); byte[] type = new byte[1];
type[0] = 0x1;
SocketClient sc = new SocketClient();
byte[] res = sc.sendMsg(type, msg); String str = new String(res, "UTF-8");
System.out.println(str);
}}

解决方案 »

  1.   

    每次调用readMsg()都会从头开始读取这个流。
    你可以用BufferedReader装饰InputStream,每次读完里面的数据就flush
      

  2.   

    首先,没看出来,使用DataInputStream和DataOutputStream这两个类,有什么具体的作用。
    楼主调用的,依然是父类的方法,所以,没有必要使用上述的两个类,
    直接使用InputStream和OutputStream类即可。产生读取重复数据的问题,一般首先要看发送端,是否重复发送了数据;其次才看接收端是否存在缓冲区。找到解决问题的思路了,就好办了。楼主可以照着这两个途径找找看。如果服务端程序没什么问题的话,那应该是DataInputStream里面内置了数据缓冲区,
    楼主在调用父类的read方法时,读取的数据,仍然在缓冲区中,未被释放。
    如果是这种情况,尝试替换该类,直接用父类即可,切忌两个类对象混着用。一般都是服务端程序重复发送了数据造成的,查看服务端的发送逻辑,解决bug即可。