英文:
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.Conn
的Read
方法从中读取数据,它会一直阻塞直到有数据可读。所以你可以为每个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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论