你如何读取TCP连接上的所有字节? [英] how do you read all the bytes that come on a tcp connection?

查看:165
本文介绍了你如何读取TCP连接上的所有字节?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在建立连接并且您想要读取来自连接的字节后,如何读取所有字节?从我所尝试的,我可以阅读,直到它像分界线一样达到分隔符。但是如何获得包括换行符在内的所有信息呢?我试图在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.

推荐答案

ioutil /#ReadAllrel =nofollow> ioutil.ReadAll ,直到 EOF 是从另一方发送或发生错误。

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.

请注意,已经有一些 redis客户端即可。

Please note that there are already some redis clients for go.

这篇关于你如何读取TCP连接上的所有字节?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆