获取非空的 net.Conn 的 Go 服务器。

huangapple go评论70阅读模式
英文:

Go server : get non-empty net.Conn

问题

我有一个带有多个net.Conn的服务器。如何获取具有未读消息的net.Conn列表(而不使用主动等待)?

英文:

I have server with several net.Conn. How can I get list of net.Conn which have unreaded messages (without using active waiting) ?

答案1

得分: 4

我不认为你能够这样做。

如果你尝试使用net.ConnRead方法从中读取数据,它会一直阻塞直到有数据可读。所以你可以为每个net.Conn启动一个goroutine,在goroutine中读取数据。

net包的文档中提供了一个示例,就是这样做的:https://pkg.go.dev/net#example-Listener

英文:

I don't think you can.

If you try to read from the net.Conn with its Read method, it will block until there is data. So just start a goroutine for each net.Conn, and read from it in the goroutine.

The example in the net package's documentation does exactly that: https://pkg.go.dev/net#example-Listener

huangapple
  • 本文由 发表于 2016年3月20日 20:08:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/36113474.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定