douluohan3403 2013-04-12 12:08
浏览 37
已采纳

去TCP超时不起作用?

I'm using net.Listen() to listen TCP connections from clients.

When client established a connection, Handler(conn net.Conn) will handle it.

func Handler(conn net.Conn) {

  read_len, err := conn.Read(request)

  if err != nil {
    if neterr, ok := err.(net.Error); ok && neterr.Timeout() {
      fmt.Println(neterr)
      PILOG("Client timeout!", PILOGWARNING)
      conn.Close()
      return
  }
}

I used a test client to connect then kill the client suddenly without sending a DISCONNECT message. My server should close the connection when reached timeout, but after a long waiting time it never happen.

I've also tried conn.SetReadDeadline(time.Now()) but seems it still not working. So I wonder what the default TCP timeout in go, and how can I set it?

I also used netstat -n and got the result below after killed the client:

tcp4 0 0 127.0.0.1.12345 127.0.0.1.57296 CLOSE_WAIT

What does CLOSE_WAIT mean?

  • 写回答

1条回答 默认 最新

  • dongyun65343 2013-04-12 12:21
    关注

    SetReadDeadline works AFAIK, but SetReadDeadline(time.Now()) has no reasonable use and that's the problem, I believe. To make a timeout of, for example N seconds from now use:

    SetReadDeadline(time.Now().Add(N*time.Second))
    

    From Wikipedia

    CLOSE-WAIT

    (both server and client) represents waiting for a connection termination request from the local user.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!