使用bufio从端口读取时出现EOF错误

时间:2017-11-10 04:02:14

标签: networking go tcp port

我正在尝试学习net包。我正在侦听端口上的本地连接,并使用echo -n "Server test.\n" | nc localhost 5000将数据发送到该端口。

但是,在读取数据时,我总是会遇到EOF错误。我检查了文档,这只应该发生when there is no more input available,但我不明白为什么会发生这种情况。

这是我的代码:

package main

import (
    "bufio"
    "fmt"
    "net"
    "os"
)

// Connection details
type connection struct {
    host    string
    port    string
    network string
}

// Initialise a Listener on a given port
// Pass handling into seperate goroutine
func main() {
    localConn := connection{
            host:    "", // Localhost
            port:    "5000",
            network: "tcp",
    }

    listener, err := net.Listen(localConn.network, localConn.host+":"+localConn.port)
    checkError("Error listening: ", err)

    conn, err := listener.Accept()
    for {
            checkError("Error accepting: ", err)
            go handleRequest(conn)
    }
}

// Delegate handling of requests
func handleRequest(conn net.Conn) {
    // Read message up until newline delimiter
    message, err := bufio.NewReader(conn).ReadString('\n')
    checkError("Error reading: ", err)

    fmt.Println("Message recieved: ", string(message))
    conn.Write([]byte("Recieved message: " + string(message) + "\n"))

    conn.Close()
}

// Check if an error exists
// If so, print and exit program. (Not super robust!)
func checkError(message string, err error) {
    if err != nil {
            fmt.Println(message, err.Error())
            os.Exit(1)
    }
}

2 个答案:

答案 0 :(得分:1)

这一方面存在一个问题:

conn, err := listener.Accept()
for {
        checkError("Error accepting: ", err)
        go handleRequest(conn)
}

应用程序在循环中启动goroutine以读取单个连接。第一个读取连接的goroutine是成功的。随后的goroutines报告错误。

将代码更改为:

for {
        conn, err := listener.Accept()
        checkError("Error accepting: ", err)
        go handleRequest(conn)
}

客户端未按服务器的预期发送换行符。使用此命令发送消息:

echo "Server test." | nc localhost 5000

答案 1 :(得分:1)

您似乎错误地使用了echo。标记-e将两个字符\n解释为换行符(选中here)。

使用以下命令将数据发送到服务器:

echo -e "Server test.\n" | nc localhost 5000

除此之外,你还应该修复for循环:

for {
    conn, err := listener.Accept()
    checkError("Error accepting: ", err)
    go handleRequest(conn)
}

在原始代码中,您只接受一个连接。之后,for循环只会启动更多尝试读取已关闭连接的goroutine(错误与否,首先handleRequest调用关闭连接)。