FAQ
Please see the code below:

package main
     import "net"
     import "log"
     import "bufio"
     import "time"

     func main() {
              l,_:=net.Listen("tcp", ":8888")
              for {
                      conn, _ := l.Accept()
                      log.Println("get conn", conn.RemoteAddr())
                      go func() {
                              f, _:=conn.(*net.TCPConn).File()
                              d:=f.Fd()
                              log.Println(d)
                              f.Close()
                              arr := make([]byte, 1000)
                              reader := bufio.NewReader(conn)
                              time.AfterFunc(3*time.Second, func() {
                                      log.Println("close conn", conn.RemoteAddr())
                                      conn.Close()
                              })
                              for {
                                      size, err := reader.Read(arr)
                                      if err != nil {
                                              break
                                      }
                                      log.Println("sss", arr[:size])
                              }
                      }()
              }
     }


when the program start,I use telnet to connect the localhost:8888,after 3
seconds,the server will kill me out, but the socket status is still
ESTABLISHED when I use netstat to watch.If I remove the File() function,
the socket can be closed normally.How can I fix this?

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Search Discussions

  • James Bardin at Mar 10, 2015 at 5:29 pm
    This looks to be caused by the socket fd being put into blocking mode.
    Blocking mode forces the Read into its own thread, and a blocking can't be
    interrupted by a close from another thread.

    I *think* it's OK to work around this by putting the FD back into
    non-blocking mode before closing:
         syscall.SetNonblock(int(d), true)

    But you can also shutdown the socket before closing via:
         conn.CloseRead()
         conn.Close()


    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedMar 10, '15 at 4:40p
activeMar 10, '15 at 5:29p
posts2
users2
websitegolang.org

2 users in discussion

Vzex1111: 1 post James Bardin: 1 post

People

Translate

site design / logo © 2021 Grokbase