我编写了一个非常简单的 TCP 服务器,它读取连接并以 HELLO WORLD 进行响应。
import (
"fmt"
"log"
"net"
)
func handleRequest(conn net.Conn) {
buff := make([]byte, 10)
_, err := conn.Read(buff)
if err != nil {
log.Fatal(err)
}
fmt.Println(string(buff))
conn.Write([]byte("HELLO WORLD"))
conn.Close()
}
func main() {
ln, err := net.Listen("tcp", ":8080")
fmt.Println("Listening on Port 8080")
if err != nil {
log.Fatal(err)
}
for {
conn, err := ln.Accept()
if err != nil {
log.Fatal(err)
}
go handleRequest(conn)
}
}
Run Code Online (Sandbox Code Playgroud)
这段代码有什么问题?当我运行 curl http://localhost:8080 时,我得到这个输出
curl: (56) Recv failure: Connection reset by peer
HELLO WORLD%
Run Code Online (Sandbox Code Playgroud)
buff:=make([]byte,1024)
Run Code Online (Sandbox Code Playgroud)
现在,如果我增加缓冲区大小,此代码可以正常工作,并且在运行 curl 后不会出现该错误。
echo -n "Hello" | nc localhost 8080
Run Code Online (Sandbox Code Playgroud)
如果我运行上面的命令,它可以正常工作。
我真的不明白这是什么原因。
Curl 发送一个大型的 HTTP 请求。尝试打印出(或log)conn.Read得到什么,你就会看到它。Curl 可能会不高兴,因为它没有得到正确的 HTTP 响应。
conn.Read 无论如何都不应该溢出缓冲区,它读取到缓冲区大小。
另一方面,管道nc仅在 TCP 套接字上发送 5 个字节。
这是一个使用正确 HTTP 的示例 TCP 服务器:
package main
import (
"bufio"
"fmt"
"log"
"net"
"strings"
)
// handleConnection handles a single connected client.
func handleConnection(c net.Conn) {
defer c.Close()
scanner := bufio.NewScanner(c)
// Scan first line for the request
if !scanner.Scan() {
log.Fatal(scanner.Err())
}
req := scanner.Text()
for scanner.Scan() {
// Scan until an empty line is seen
if len(scanner.Text()) == 0 {
break
}
}
fmt.Println("req:", req)
if strings.HasPrefix(req, "GET") {
rt := fmt.Sprintf("HTTP/1.1 200 Success\r\n")
rt += fmt.Sprintf("Connection: Close\r\n")
rt += fmt.Sprintf("Content-Type: text/html\r\n\r\n")
rt += fmt.Sprintf("<html><body>Nothing here</body></html>\r\n")
c.Write([]byte(rt))
} else {
rt := fmt.Sprintf("HTTP/1.1 %v Error Occurred\r\n\r\n", 501)
c.Write([]byte(rt))
}
}
func main() {
l, err := net.Listen("tcp", ":8080")
if err != nil {
log.Fatal(err)
}
defer l.Close()
for {
// Wait for a connection.
conn, err := l.Accept()
if err != nil {
log.Fatal(err)
}
go handleConnection(conn)
}
}
Run Code Online (Sandbox Code Playgroud)