如何读取TCP连接上的所有字节?

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

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.

huangapple
  • 本文由 发表于 2012年9月26日 22:50:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/12604464.html
匿名

发表评论

匿名网友

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

确定