dongzhan7909
2016-12-28 05:49
浏览 177
已采纳

在Golang中设置连接生命的时间

I am a novice in golang and I am writing a client-server application through the TCP protocol. I need to make a temporary connection, which will close after a couple of seconds. I don't understand how to do that.
I have a such function, which creates a connection and waits for gob data:

    func net_AcceptAppsList(timesleep time.Duration) {
        ln, err := net.Listen("tcp", ":"+conf.PORT)
        CheckError(err)
        conn, err := ln.Accept()
        CheckError(err)
        dec := gob.NewDecoder(conn)
        pack := map[string]string{}
        err = dec.Decode(&pack)
        fmt.Println("Message:", pack)
        conn.Close()
}

I need to make this function to wait for data for only some seconds - not forever.

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

2条回答 默认 最新

相关推荐 更多相似问题