解决方案 »

  1.   


    Socket socket = new Socket(ip,port);
    InputStream ins = socket.getInpuStream();
    byte[] b = readInputStream(ins );public static byte[] readInputStream(InputStream ins) {  
        if (ins == null) {  
            return null;  
        }  
        BufferedInputStream bis = new BufferedInputStream(ins);  
        ByteArrayOutputStream bos = new ByteArrayOutputStream();  
        try {  
            byte[] buffer = new byte[128];  
            int n = -1;  
            while ((n = bis.read(buffer)) != -1) {  
                bos.write(buffer, 0, n);  
            }  
        } catch (IOException e) {  
            e.printStackTrace();  
            return null;  
        } finally {  
            if (bis != null) {  
                try {  
                    bis.close();  
                } catch (IOException e) {  
                    e.printStackTrace();  
                }  
            }  
        }  
        return bos.toByteArray();  
    } socket是比较稳定的,不太可能出现数据增加的问题,除非服务器主动发送了过多的数据。
      

  2.   


    这种我开始做的时候就是用这种,  while ((n = bis.read(buffer)) != -1) {  
                bos.write(buffer, 0, n);  
            }  
    会导致线程阻塞,当流读取完的时候在执行bis.read(buffer),就会阻塞了,不知道是不是我理解错误