为什么我可以成功将消息发布到已关闭的服务器?

时间:2018-04-27 06:44:45

标签: sockets go tcp

func main() {
    server := "localhost:8989"
    tcpAddr, err := net.ResolveTCPAddr("tcp4", server)
    if err != nil {
        fmt.Fprintf(os.Stderr, "Fatal error: %s", err.Error())
        os.Exit(1)
    }

    conn, err := net.DialTCP("tcp", nil, tcpAddr)
    if err != nil {
        fmt.Fprintf(os.Stderr, "Fatal error: %s", err.Error())
        os.Exit(1)
    }

    fmt.Println("connect success")
    for i := 0; i < 10; i++ {
        j, err := conn.Write([]byte("hello"))
        if err != nil {
            fmt.Printf("conn.Write err: %v\n", err)
            continue
        }
        fmt.Printf("time[%v] conn.Write len: %v\n", i+1, j)
        time.Sleep(5 * time.Second)
    }
}

我使用demo将10条消息发布到tcp服务器。我明白了:

    connect success
time[1] conn.Write len: 5
time[2] conn.Write len: 5
time[3] conn.Write len: 5
conn.Write err: write tcp 127.0.0.1:52462->127.0.0.1:8989: wsasend: An established connection was aborted by the software in your host machine.
conn.Write err: write tcp 127.0.0.1:52462->127.0.0.1:8989: wsasend: An established connection was aborted by the software in your host machine.
conn.Write err: write tcp 127.0.0.1:52462->127.0.0.1:8989: wsasend: An established connection was aborted by the software in your host machine.
conn.Write err: write tcp 127.0.0.1:52462->127.0.0.1:8989: wsasend: An established connection was aborted by the software in your host machine.
conn.Write err: write tcp 127.0.0.1:52462->127.0.0.1:8989: wsasend: An established connection was aborted by the software in your host machine.
conn.Write err: write tcp 127.0.0.1:52462->127.0.0.1:8989: wsasend: An established connection was aborted by the software in your host machine.
conn.Write err: write tcp 127.0.0.1:52462->127.0.0.1:8989: wsasend: An established connection was aborted by the software in your host machine.

我在第三条消息之前关闭了服务器。但第三条消息成功发布。 conn.Write err得到零。我无法理解。谁能告诉我为什么?感谢。

0 个答案:

没有答案