server start... Read 8 bit // 第一次读取到8个字节 byte:0 add into maxBuf[0] byte:0 add into maxBuf[1] byte:0 add into maxBuf[2] byte:2 add into maxBuf[3] byte:1 add into maxBuf[4] byte:2 add into maxBuf[5] byte:0 add into maxBuf[6] byte:0 add into maxBuf[7] msg body len is:2 msg head = 00000002. body = 0102 12//这里读取到第一条消息, 内容是1,2, 正确 print content done byte:0 add into maxBuf[0] byte:0 add into maxBuf[1] newOffser = 2// 读取了8个字节解析了第一条消息, 一共用去了6个字节, 剩下2个字节放回缓冲数组
Read 3 bit // 第二次读取到3个字节, 加上上面剩余的2个一共就有5个字节 byte:0 add into maxBuf[2] byte:5 add into maxBuf[3] byte:101 add into maxBuf[4] msg body len is:5 bufOffset < msgHeadLen+int(bodyLen) // 程序解析得到第二条消息的body长度是5, 完整的数据就是4+5=9个字节, 而我们才5个字节明显不够, 继续等待新数据的到来
Read 6 bit // 第三次读取, 读取到6个字节 byte:102 add into maxBuf[5] byte:103 add into maxBuf[6] byte:104 add into maxBuf[7] byte:105 add into maxBuf[8] byte:0 add into maxBuf[9] byte:0 add into maxBuf[10] msg body len is:5 msg head = 00000005. body = 6566676869 101102103104105// 解析得到第二条消息的内容101,102,103,104,105, 正确 print content done byte:0 add into maxBuf[0] byte:0 add into maxBuf[1] newOffser = 2//将剩余的2个字节放回缓冲数组
Read 1 bit // 第4次读取到1个字节, 加上上面剩余的2个一共3个 byte:0 add into maxBuf[2] totalCount < msgHeadLen // 3个字节很明显不够一条消息的头部长度, 继续等待读取
Read 2 bit // 第5次读取到2个字节, 加上上面剩余的3个, 一共5个字节 byte:1 add into maxBuf[3] byte:108 add into maxBuf[4] msg body len is:1 msg head = 00000001. body = 6c 108// 解析得到第三条消息内容是108, 正确 print content done newOffser = 0// 刚好5个字节都用完了, 剩余0个字节.