日期:2014-05-20 浏览次数:20778 次
/**
* 报文头固定5位为报文长度,按长度接收
* @param socket
* @return 接收到的报文
*/
public static byte[] accept(Socket socket){
byte [] head = new byte[5];
byte [] body=null;
try {
BufferedInputStream bufIn = new BufferedInputStream(socket.getInputStream());
bufIn.read(head);
System.out.println("head "+new String(head));
int len1 = Integer.parseInt(new String(head));
body = new byte[len1];
bufIn.read(body);
} catch (IOException e) {
e.printStackTrace();
}
return body;
}
byte[] res = SocketUtil.accept(socket);//读取服务器端返回bytes
String str=new String(res);//转化为String
str=ByteOrStringHelper.ByteToString(res,"GBK");//根据编码格式转化
System.out.println("读取服务器返回数据:"+str);