网络是有延迟的,当从网络上读取数据流时,一般都这样写:
int buflen;//需要读取的字节长度
int readed;//已经读取的字节长度
readed = 0;
while (readed < buflen) readed = dataIn.read(readByte,readed,buflen-readed)
:)
good luck!

解决方案 »

  1.   

    没考虑网络延迟,给你一个例子看看
    public class ServerTest {
    public static void main(String[] args) throws Exception {
    ServerSocket ss = new ServerSocket(9919);
    Socket sock = ss.accept();
    InputStream in = sock.getInputStream();
    OutputStream out = sock.getOutputStream();
    for (int i = 0; i < 10; i++) {
    out.write(1);
    Thread.sleep(1000);
    }
    sock.close();
    }
    }public class ClientTest {
    public static void main(String[] args) throws Exception {
    Socket sock = new Socket("localhost", 9919);
    InputStream in = sock.getInputStream();
    byte[] c = new byte[10];
    in.read(c, 0, 10);
    for (int i = 0; i < c.length; i++) {
    System.out.println(c[i]);
    }

    sock.close();
    }
    }
    这个例子,你要是把Thread.sleep()去掉就对了,加上sleep来模拟网络延迟的话,客户端就要变成楼上的写法,比如
    while (readed < 10) readed = in.read(c, readed, 10 - readed)