douyu9159 2018-12-23 00:16
浏览 483

Go Golang net.Dial发送1个请求,但读取第二个响应

I am using Go to read a reply from a sever using net.Dial. The code below works but the server sends back 2 replys. To read the second reply I would have to read the reply again. Is there an easier way to discard the first reply and grab the second reply? Regards

_, err = conn.Write([]byte(login))
reply := make([]byte, 5000)

_, err = conn.Read(reply)
fmt.Print(string(reply))
  • 写回答

1条回答 默认 最新

  • douza19870617 2018-12-23 03:15
    关注
    io.CopyN(ioutil.Discard, conn, 5000)
    

    it will read the first 5000 bytes and discard them. This is assuming each reply is exactly 5000 bytes though.

    If the reply's are strings separated by new lines (such as http or irc), you could use buffio

    reader := bufio.NewReader(os.Stdin)
    reader.ReadString('
    ')
    secondline := reader.ReadString('
    ')
    
    评论

报告相同问题?

悬赏问题

  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了