package tcp_packet import ( "fmt" "net" lift "lift-monitor/handler/v1" ) func monitorDataHandel(src []byte) (res []byte, sn string, err error) { // 解析数据帧 res, sn, err = lift.LiftFrameParse(src) if err != nil { fmt.Printf("LiftFrameParse:%v\n", err) } return res, sn, err } func HandleSocketClient(conn net.Conn) { // 处理完后关闭连接 defer conn.Close() request := make([]byte, 1024) var gleft, result []byte var isComplete bool for { read_len, err := conn.Read(request) fmt.Printf("read read:%v,%v,%s\n", read_len, err, request[:read_len]) if err != nil { break } gleft = append(gleft, request[:read_len]...) for { result, gleft, isComplete, err = lift.LiftParseTcp(gleft[:]) fmt.Printf("is complete:%v, err:%v\n", isComplete, err) if err != nil { return } // 包没有读完继续读取数据包 if isComplete == false { break } fmt.Printf("complete:%s\n", result) // 处理数据 res, _, err := monitorDataHandel(result) if err != nil { return } conn.Write(res) } } }