dsjq62428 2018-03-06 17:26
浏览 98
已采纳

如何停止从UDP读取的goroutine?

I have a go program that uses a goroutine to read UDP packets. I wanted to use a select clause and a "stopping" channel to close the goroutine to shut down as soon as it is not needed anymore.

Here is a simple code example for the goroutine:

func Run(c chan string, q chan bool, conn *net.UDPConn) {

    defer close(c)

    buf := make([]byte, 1024)

    for {
        select {
            case <- q:
                return
            default:
                n, _, err := conn.ReadFromUDP(buf)
                c <- string(buf[0:n])
                fmt.Println("Received ", string(buf[0:n]))

                if err != nil {
                    fmt.Println("Error: ", err)
                }
        }
    }
}

The connection is created as:

    conn, err := net.ListenUDP("udp",addr.Addr)

And the goroutine is supposed to terminate using:

    close(q)

After closing the "stopping" channel ("q") the goroutine does not immediately stop. I need to send one more string via the UDP connection. When doing so the goroutine stops. I simply do not understand this behaviour and I would be grateful if somebody could enlighten me.

Thank you in advance!

  • 写回答

1条回答 默认 最新

  • duanchu3376 2018-03-06 18:25
    关注

    Your program is likely stopped at this line when you close the channel:

    n, _, err := conn.ReadFromUDP(buf)
    

    Because execution is blocked at a ReadFrom method, the select statement is not being evaluated, therefore the close on channel q is not immediately detected. When you do another send on the UDP connection, ReadFrom unblocks and (once that loop iteration finishes) control moves to the select statement: at that point the close on q is detected.

    You can close the connection to unblock ReadFrom, as was suggested in a comment. See the PacketConn documentation in the net package, especially "Any blocked ReadFrom or WriteTo operations will be unblocked and return errors":

    // Close closes the connection.
    // Any blocked ReadFrom or WriteTo operations will be unblocked and return errors.
    Close() error
    

    Depending on your needs a timeout might be an option as well, see again PacketConn documentation in the net package:

     // ReadFrom reads a packet from the connection,
     // copying the payload into b. It returns the number of
     // bytes copied into b and the return address that
     // was on the packet.
     // ReadFrom can be made to time out and return
     // an Error with Timeout() == true after a fixed time limit;
     // see SetDeadline and SetReadDeadline.
     ReadFrom(b []byte) (n int, addr Addr, err error)
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题