英文:
how do you read all the bytes that come on a tcp connection?
问题
在建立连接后,当您想要读取来自连接的字节时,如何读取所有字节?根据我的尝试,我可以读取直到达到像换行符这样的分隔符。但是如何获取包括换行符在内的所有内容呢?我正在尝试使用Go语言制作一个Redis客户端,该协议使用\r\n分隔结果,所以在这种情况下,Buffer.ReadLine或Buffer.ReadSlice没有帮助。
英文:
After you establish a connection and you want to read the bytes coming from the connection, how can you read all the bytes? From what I tried, I can read until it reaches a delimiter like a line break. But how is it possible to get everything including the line breaks? I'm trying to make a redis client in Go and the protocol separates results with \r\n, so in this case Buffer.ReadLine, or Buffer.ReadSlice didn't help.
答案1
得分: 3
要从Reader(比如你的TCP连接)中读取所有字节,你可以使用ioutil.ReadAll
函数,它会一直读取直到另一端发送了EOF
或者发生了错误。
请注意,已经有一些适用于Go语言的redis客户端可供使用。
英文:
To read all bytes from a Reader (like your TCP connection), you can use ioutil.ReadAll
, which reads until an EOF
is sent from the other side or an error occurs.
Please note that there are already some redis clients for go.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论