dongli5785
2019-06-16 15:15
浏览 165
已采纳

如何循环直到* ipconn.Read()读取go中发送给它的所有数据

I was sending some data using *ipconn.Write method in go , but it seems *ipconn.Read() can only read 20 bytes at a time

here is server sending data

ln, err := net.Listen("tcp", "localhost:8888")
conn, err := ln.Accept()
tmp := make([]byte,10000)
tmp = []byte("abcdefghijklmnopqrstuvwxyz")
conn.Write(tmp)

here is the client receiving data

conn, err := net.Dial("tcp", "localhost:8888")
data := make([]byte, 100000)
conn.Read(data)
fmt.Println(string(data)) // prints only first 20 chars

If i again call conn.Read(data) I get another 20 characters

Is there any way to read all the data or loop until the connection is closed ?

图片转代码服务由CSDN问答提供 功能建议

我在go中使用* ipconn.Write方法发送了一些数据,但看来* ipconn.Read()可以 一次只能读取20个字节

服务器正在发送数据

  ln,err  := net.Listen(“ tcp”,“ localhost:8888”)
conn,err:= ln.Accept()
tmp:= make([] byte,10000)
tmp = [] byte(“ abcdefghijklmnopqrstuvwxyz”)  
conn.Write(tmp)
   
 
 

这里是接收数据的客户端

  conn,错误:= net.Dial(“ tcp”,“ localhost:8888”)
data:= make([] byte,100000)
conn.Read(data)
fmt.Println(string(data  ))//仅打印前20个字符
   
 
 

如果我再次调用conn.Read(data),我又得到了20个字符 < p>在关闭连接之前,有什么方法可以读取所有数据或循环吗?

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dongyi0114 2019-06-18 06:21
    已采纳

    ioutil.ReadAll will read all the data from the tcp stream. Be careful as it doesnt return until the connection has been closed.

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题