Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/go/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Networking 使用bufio从端口读取时出现EOF错误_Networking_Go_Tcp_Port - Fatal编程技术网

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

Networking 使用bufio从端口读取时出现EOF错误,networking,go,tcp,port,Networking,Go,Tcp,Port,我正在努力学习net软件包。我正在侦听端口上的本地连接,并使用echo-n“服务器测试”将数据发送到该端口。\n“|nc localhost 5000 然而,在读取数据时,我总是得到一个EOF错误。我检查了文档,这只是应该发生的,但是我不明白为什么会发生这种情况 这是我的代码: package main import ( "bufio" "fmt" "net" "os" ) // Connection details type connection struc

我正在努力学习
net
软件包。我正在侦听端口上的本地连接,并使用
echo-n“服务器测试”将数据发送到该端口。\n“|nc localhost 5000

然而,在读取数据时,我总是得到一个
EOF
错误。我检查了文档,这只是应该发生的,但是我不明白为什么会发生这种情况

这是我的代码:

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)
    }
}

其中一个问题是:

conn, err := listener.Accept()
for {
        checkError("Error accepting: ", err)
        go handleRequest(conn)
}
应用程序在循环中启动goroutines以读取单个连接。读取连接的第一个goroutine成功。随后的goroutine报告一个错误

将代码更改为:

for {
        conn, err := listener.Accept()
        checkError("Error accepting: ", err)
        go handleRequest(conn)
}
客户端未按服务器的预期发送换行符。使用此命令发送消息:

echo "Server test." | nc localhost 5000

其中一个问题是:

conn, err := listener.Accept()
for {
        checkError("Error accepting: ", err)
        go handleRequest(conn)
}
应用程序在循环中启动goroutines以读取单个连接。读取连接的第一个goroutine成功。随后的goroutine报告一个错误

将代码更改为:

for {
        conn, err := listener.Accept()
        checkError("Error accepting: ", err)
        go handleRequest(conn)
}
客户端未按服务器的预期发送换行符。使用此命令发送消息:

echo "Server test." | nc localhost 5000

您似乎不正确地使用了
echo
。标志
-e
将两个字符
\n
解释为换行符(检查)

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

echo -e "Server test.\n" | nc localhost 5000
除此之外,您还应该为循环修复

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

在原始代码中,只有一个连接被接受。在此之后,for循环只会启动更多的goroutine,尝试在关闭的连接上读取(无论是否出错,首先
handleRequest
调用会关闭连接)。

您似乎没有正确使用
echo
。标志
-e
将两个字符
\n
解释为换行符(检查)

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

echo -e "Server test.\n" | nc localhost 5000
除此之外,您还应该为
循环修复

for {
    conn, err := listener.Accept()
    checkError("Error accepting: ", err)
    go handleRequest(conn)
}
在原始代码中,只有一个连接被接受。在此之后,for循环只会启动更多的goroutine,这些goroutine尝试在一个关闭的连接上读取数据(无论是否出错,首先
handleRequest
调用关闭连接)