This commit is contained in:
邹宗楠
2023-06-05 15:34:44 +08:00
parent fec4011989
commit 765517013c

View File

@@ -10,6 +10,7 @@ import (
"git.rosy.net.cn/jx-callback/business/model/dao"
"git.rosy.net.cn/jx-callback/globals"
"io"
"io/ioutil"
"net"
"strings"
"time"
@@ -76,6 +77,21 @@ func ListenTcp() {
}
}
func ConReadAll(c net.Conn) ([]byte, int, error) {
data, err := ioutil.ReadAll(c)
return data, len(data), err
}
func ConnRead(c net.Conn) ([]byte, int, error) {
buffer := make([]byte, 1024)
n, err := c.Read(buffer)
return buffer, n, err
}
func ConnReadFull(c net.Conn) ([]byte, int, error) {
buffer := make([]byte, 1024)
n, err := io.ReadFull(c, buffer)
return buffer, n, err
}
func handleConn(c net.Conn, t *TcpClient) error {
if c == nil {
return errors.New("conn is nil")
@@ -84,9 +100,20 @@ func handleConn(c net.Conn, t *TcpClient) error {
for {
var (
printNo string //打印机编号
buffer = make([]byte, 1024)
//buffer = make([]byte, 1024)
)
n, err := c.Read(buffer)
// 第一种
buffer, n, err := ConReadAll(c)
globals.SugarLogger.Debugf("ConReadAll : %s,%d,%v", string(buffer), n, err)
// 第二种
//buffer, n, err := ConnReadFull(c)
//globals.SugarLogger.Debugf("ConnReadFull : %s,%d,%v", string(buffer), n, err)
//// 第三种(正常)
//buffer, n, err := ConnRead(c)
//globals.SugarLogger.Debugf("ConnRead : %s,%d,%v", string(buffer), n, err)
//n, err := c.Read(buffer)
if err != nil {
if err == io.EOF {
fmt.Println("connection close")