我们的程序读Socket数据,发现当服务器端发送大于一定字节的数据时,客户端代码总是读不全数据,很急,请大家帮看看。小数据时候没有问题。大数据也不是特别的大就几十K .protected static byte[] readInputStream(InputStream _in) throws IOException {
byte data[] = null;
if(_in==null)
throw new IOException("InputStream can't be null!");
int s = _in.read();
if(s==-1) {
return null; //Connection lost
}
int alength = _in.available();
if(alength > 0) {
data = new byte[alength+1];
_in.read(data, 1, alength);
} else {
data = new byte[1];
}
data[0] = (byte)s;
return data;
}
byte data[] = null;
if(_in==null)
throw new IOException("InputStream can't be null!");
int s = _in.read();
if(s==-1) {
return null; //Connection lost
}
int alength = _in.available();
if(alength > 0) {
data = new byte[alength+1];
_in.read(data, 1, alength);
} else {
data = new byte[1];
}
data[0] = (byte)s;
return data;
}
可用如下方法:
int len;
while((len=_in.read(data))!=-1){
//相关的处理
}
while((length = inputStream.read(buffer)) > 0)//
{
out.write(buffer,0,length);//Here
}