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

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
", err)
            continue
        }
        fmt.Printf("time[%v] conn.Write len: %v
", i+1, j)
        time.Sleep(5 * time.Second)
    }
}

I post 10 messages to tcp server with the demo. And I get this:

    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.

I closed the server before the third message. But the third message post successfully. The conn.Write err get nil. I can't understand. Who can tell me why? Thanks.

可以尝试将缓冲区关闭试试