golang TCPConn.SetWriteDeadline似乎没有按预期工作

时间:2013-02-25 12:44:46

标签: tcp network-programming go

我试图通过检查golang TCPConn.Write返回的错误来检测发送失败,但它没有。我也尝试使用TCPConn.SetWriteDeadline但没有成功。

事情就是这样:

  1. 服务器启动
  2. 客户端连接
  3. 服务器发送消息,客户端接收消息
  4. 客户关闭
  5. 服务器再发送一条消息:无错误
  6. 服务器发送第三条消息:现在只出现错误
  7. 问题:为什么只有第二封邮件发送给不存在的客户端会导致错误?如何正确处理案件?

    代码如下:

    package main
    
    import (
        "net"
        "os"
        "bufio"
        "fmt"
        "time"
    )
    
    func AcceptConnections(listener net.Listener, console <- chan string) {
    
        msg := ""
    
        for {
    
            conn, err := listener.Accept()
    
            if err != nil {
                panic(err)
            }
    
            fmt.Printf("client connected\n")
    
            for {
    
                if msg == "" {
                    msg = <- console
                    fmt.Printf("read from console: %s", msg)
                }
    
                err = conn.SetWriteDeadline(time.Now().Add(time.Second))
    
                if err != nil {
                    fmt.Printf("SetWriteDeadline failed: %v\n", err)
                }
    
                _, err = conn.Write([]byte(msg))
    
                if err != nil {
                    // expecting an error after sending a message
                    // to a non-existing client endpoint
                    fmt.Printf("failed sending a message to network: %v\n", err)
                    break
                } else {
                    fmt.Printf("msg sent: %s", msg)
                    msg = ""
                }
            }
        }
    }
    
    func ReadConsole(network chan <- string) {
    
        console := bufio.NewReader(os.Stdin)
    
        for {
    
            line, err := console.ReadString('\n')
    
            if err != nil {
    
                panic(err)
    
            } else {
    
                network <- line
            }
        }
    }
    
    func main() {
    
        listener, err := net.Listen("tcp", "localhost:6666")
    
        if err != nil {
            panic(err)
        }
    
        println("listening on " + listener.Addr().String())
    
        consoleToNetwork := make(chan string)
    
        go AcceptConnections(listener, consoleToNetwork)
    
        ReadConsole(consoleToNetwork)
    }
    

    服务器控制台如下所示:

    listening on 127.0.0.1:6666
    client connected
    hi there!
    read from console: hi there!
    msg sent: hi there!
    this one should fail
    read from console: this one should fail
    msg sent: this one should fail
    this one actually fails
    read from console: this one actually fails
    failed sending a message to network: write tcp 127.0.0.1:51194: broken pipe
    

    客户端看起来像这样:

    package main
    
    import (
        "net"
        "os"
        "io"
        //"bufio"
        //"fmt"
    )
    
    func cp(dst io.Writer, src io.Reader, errc chan<- error) {
    
        // -reads from src and writes to dst
        // -blocks until EOF
        // -EOF is not an error
        _, err :=  io.Copy(dst, src)
    
        // push err to the channel when io.Copy returns
        errc <- err
    }
    
    func StartCommunication(conn net.Conn) {
    
        //create a channel for errors
        errc := make(chan error)
    
        //read connection and print to console
        go cp(os.Stdout, conn, errc)
    
        //read user input and write to connection
        go cp(conn, os.Stdin, errc)
    
        //wait until nil or an error arrives
        err := <- errc
    
        if err != nil {
            println("cp error: ", err.Error())
        }
    }
    
    func main() {
    
        servAddr := "localhost:6666"
    
        tcpAddr, err := net.ResolveTCPAddr("tcp", servAddr)
    
        if err != nil {
            println("ResolveTCPAddr failed:", err.Error())
            os.Exit(1)
        }
    
        conn, err := net.DialTCP("tcp", nil, tcpAddr)
    
        if err != nil {
            println("net.DialTCP failed:", err.Error())
            os.Exit(1)
        }
    
        defer conn.Close()
    
        StartCommunication(conn)
    
    }
    

    编辑:根据JimB的建议,我想出了一个有效的例子。消息不会再丢失,并在新连接中重新发送。我不太确定在不同的例程之间使用共享变量(connWrap.IsFaulted)有多安全。

    package main
    
    import (
        "net"
        "os"
        "bufio"
        "fmt"
    )
    
    type Connection struct {
        IsFaulted bool
        Conn net.Conn
    }
    
    func StartWritingToNetwork(connWrap * Connection, errChannel chan <- error, msgStack chan string) {
    
        for {
    
            msg := <- msgStack
    
            if connWrap.IsFaulted {
    
                //put it back for another connection
                msgStack <- msg
    
                return
            }
    
            _, err := connWrap.Conn.Write([]byte(msg))
    
            if err != nil {
    
                fmt.Printf("failed sending a message to network: %v\n", err)
    
                connWrap.IsFaulted = true
    
                msgStack <- msg
    
                errChannel <- err
    
                return
    
            } else {
    
                fmt.Printf("msg sent: %s", msg)
            }
        }
    }
    
    func StartReadingFromNetwork(connWrap * Connection, errChannel chan <- error){
    
        network := bufio.NewReader(connWrap.Conn)
    
        for (!connWrap.IsFaulted) {
    
            line, err := network.ReadString('\n')
    
            if err != nil {
    
                fmt.Printf("failed reading from network: %v\n", err)
    
                connWrap.IsFaulted = true
    
                errChannel <- err
    
            } else {
    
                fmt.Printf("%s", line)
            }
        }
    }
    
    func AcceptConnections(listener net.Listener, console chan string) {
    
        errChannel := make(chan error)
    
        for {
    
            conn, err := listener.Accept()
    
            if err != nil {
                panic(err)
            }
    
            fmt.Printf("client connected\n")
    
            connWrap := Connection{false, conn}
    
            go StartReadingFromNetwork(&connWrap, errChannel)
    
            go StartWritingToNetwork(&connWrap, errChannel, console)
    
            //block until an error occurs
            <- errChannel
        }
    }
    
    func ReadConsole(network chan <- string) {
    
        console := bufio.NewReader(os.Stdin)
    
        for {
    
            line, err := console.ReadString('\n')
    
            if err != nil {
    
                panic(err)
    
            } else {
    
                network <- line
            }
        }
    }
    
    func main() {
    
        listener, err := net.Listen("tcp", "localhost:6666")
    
        if err != nil {
            panic(err)
        }
    
        println("listening on " + listener.Addr().String())
    
        consoleToNetwork := make(chan string)
    
        go AcceptConnections(listener, consoleToNetwork)
    
        ReadConsole(consoleToNetwork)
    }
    

1 个答案:

答案 0 :(得分:11)

这不是特定于Go的,并且是底层TCP套接字的工件。

TCP终止步骤的合适图表位于此页面的底部: http://www.tcpipguide.com/free/t_TCPConnectionTermination-2.htm

简单版本是当客户端关闭其套接字时,它发送FIN,并从服务器接收ACK。然后它等待服务器执行相同的操作。虽然不是发送FIN,而是发送更多数据,而这些数据被丢弃,而客户端套接字现在假定来自您的更多数据无效,因此下次发送时会获得RST,这就是冒泡你看到的错误。

回到你的程序,你需要以某种方式处理它。通常你可以想到谁负责发起一个发送,也负责启动发送,因此你的服务器应该假设它可以继续发送,直到关闭连接,或遇到错误。如果需要更可靠地检测客户端关闭,则需要在协议中进行某种客户端响应。这样可以在套接字上调用recv并返回0,它会提醒您关闭连接。

在go中,这将从连接的Read方法(或在您的情况下从Copy中)返回EOF错误。 SetWriteDeadline不起作用,因为一个小的写入会通过并静默删除,或者客户端最终将使用RST响应,从而导致错误。