读书人

java 网络读取屡次readdemo

发布时间: 2012-10-12 10:17:04 作者: rapoo

java 网络读取,多次read,demo

public byte[] doConnNetWork(byte[] outputBytes) throws IOException {byte[] result = null; Socket connection = null;OutputStream   outputStream = null;InputStream    inputStream = null; int c = 0;boolean isGo = true; // 是否继续// 只讲重点的,其他的忽略掉了    connection =  openConnection();     inputStream = this.openConnectionInputStream(connection);     outputStream = this.openConnectionOutputStream(connection);  //***  从服务器端接受数据  ***//  while (c < 3 && isGo) { //--------------------------------------------- 读取数据核心代码 start ----------------------------------int hLen = 18;byte[] header = new byte[hLen];inputStream.read(header, 0, hLen);// 读取包头ResponseHeader rheader = DataPackage.getResponseHeader(header);int len = rheader.getDwDataLen();REQUEST_TYPE = rheader.getRequestType();// 解析header,得到数据长度lenbyte[] content = new byte[len];while (inputStream.available() < len) {try {Thread.sleep(10);//System.out.println(" len --> " + len); } catch (InterruptedException e) { e.printStackTrace();}}  inputStream.read(content, 0, len);//int rLen =  //System.out.println(" len --> " + len + "  read Len ->" + rLen);if (len > 0) {result = new byte[hLen + len];System.arraycopy(header, 0, result, 0, hLen);System.arraycopy(content, 0, result, hLen, len);} else {result = header;}//---------------------------------------------- 读取数据核心代码 end --------------------------------------    c++;} closeConnection(connection, outputStream, inputStream);   return result;} 

?

读书人网 >编程

热点推荐