From 4cd4559784908df3205bd60515ffd4faeaff8551 Mon Sep 17 00:00:00 2001 From: Anson <77931774@qq.com> Date: Thu, 12 Aug 2021 00:21:36 +0800 Subject: [PATCH 1/3] fix tcp demo error content-length --- .example/net/gtcp/gtcp_conn.go | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/.example/net/gtcp/gtcp_conn.go b/.example/net/gtcp/gtcp_conn.go index a312c4ead..4008053ef 100644 --- a/.example/net/gtcp/gtcp_conn.go +++ b/.example/net/gtcp/gtcp_conn.go @@ -6,7 +6,7 @@ import ( "os" "github.com/gogf/gf/net/gtcp" - "github.com/gogf/gf/util/gconv" + "strconv" ) func main() { @@ -16,7 +16,7 @@ func main() { } defer conn.Close() - if err := conn.Send([]byte("GET / HTTP/1.1\n\n")); err != nil { + if err := conn.Send([]byte("GET / HTTP/1.1\r\n\r\n")); err != nil { panic(err) } @@ -30,13 +30,17 @@ func main() { array := bytes.Split(data, []byte(": ")) // 获得页面内容长度 if contentLength == 0 && len(array) == 2 && bytes.EqualFold([]byte("Content-Length"), array[0]) { - contentLength = gconv.Int(array[1]) + // http 以\r\n换行,需要把\r也去掉 + contentLength, err = strconv.Atoi(string(array[1][:len(array[1])-1])) + if err != nil { + fmt.Println(err) + } } header = append(header, data...) header = append(header, '\n') } - // header读取完毕,读取文本内容 - if contentLength > 0 && len(data) == 0 { + // header读取完毕,读取文本内容, 1为\r + if contentLength > 0 && len(data) == 1 { content, _ = conn.Recv(contentLength) break } From 2bd76dfdded37c0c9da71c20211d17503327099c Mon Sep 17 00:00:00 2001 From: ansionfor <77931774@qq.com> Date: Thu, 12 Aug 2021 09:35:57 +0800 Subject: [PATCH 2/3] go fmt code --- .example/net/gtcp/gtcp_conn.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/.example/net/gtcp/gtcp_conn.go b/.example/net/gtcp/gtcp_conn.go index 4008053ef..7cd01001e 100644 --- a/.example/net/gtcp/gtcp_conn.go +++ b/.example/net/gtcp/gtcp_conn.go @@ -32,9 +32,9 @@ func main() { if contentLength == 0 && len(array) == 2 && bytes.EqualFold([]byte("Content-Length"), array[0]) { // http 以\r\n换行,需要把\r也去掉 contentLength, err = strconv.Atoi(string(array[1][:len(array[1])-1])) - if err != nil { - fmt.Println(err) - } + if err != nil { + fmt.Println(err) + } } header = append(header, data...) header = append(header, '\n') From 91cd4f96f0b9b9ad464b50c2d8de9bdc34b9e07f Mon Sep 17 00:00:00 2001 From: Anson <77931774@qq.com> Date: Thu, 12 Aug 2021 10:50:46 +0800 Subject: [PATCH 3/3] Update gtcp_conn.go --- .example/net/gtcp/gtcp_conn.go | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/.example/net/gtcp/gtcp_conn.go b/.example/net/gtcp/gtcp_conn.go index 7cd01001e..323595751 100644 --- a/.example/net/gtcp/gtcp_conn.go +++ b/.example/net/gtcp/gtcp_conn.go @@ -6,7 +6,7 @@ import ( "os" "github.com/gogf/gf/net/gtcp" - "strconv" + "github.com/gogf/gf/util/gconv" ) func main() { @@ -31,10 +31,7 @@ func main() { // 获得页面内容长度 if contentLength == 0 && len(array) == 2 && bytes.EqualFold([]byte("Content-Length"), array[0]) { // http 以\r\n换行,需要把\r也去掉 - contentLength, err = strconv.Atoi(string(array[1][:len(array[1])-1])) - if err != nil { - fmt.Println(err) - } + contentLength = gconv.Int(string(array[1][:len(array[1])-1])) } header = append(header, data...) header = append(header, '\n')