一种Go使用tcp检测超时的方式

c.SetReadDeadline(time.Now())
if _, err := c.Read(one); err == io.EOF {
  l.Printf(logger.LevelDebug, "%s detected closed LAN connection", id)
  c.Close()
  c = nil
} else {
  var zero time.Time
  c.SetReadDeadline(time.Now().Add(10 * time.Millisecond))
}
For detecting a timeout, it suggests:

if neterr, ok := err.(net.Error); ok && neterr.Timeout() {
  //......
}

Source Page: http://stackoverflow.com/a/12741495

 
posted @ 2016-06-29 08:55  Waichung  阅读(1813)  评论(0编辑  收藏  举报