du0173 2018-10-26 04:17
浏览 91
已采纳

当我从tcp缓冲区读取时,应该为mtu值选择哪个数字

Here is my code with golang when I'm ready to read buffer:

func Listen() {
  listen, _ := net.Listen("tcp4", "127.0.0.1:7000")
  defer listen.Close()
  for {
    conn, _ := listen.Accept()
    go handler(&conn)
  }
}

func handler(c *net.Conn) {
  for {
    buf := make([]byte, 1464)
    n, _ := (*c).Read(buf)
    if n == 0 {
      continue
    }
  }
}

The 1464 is my network mtu 1492 - 28 IP Header,so which number should I input here?

My network uses ppoe protocol, so the default is (1518 - 18 - 8) 8 is ppoe protocol header, rasult is 1492, then which number should I input there 1492 or 1464?(1492-28)

  • 写回答

1条回答 默认 最新

  • doushaizhen1244 2018-10-26 07:48
    关注

    In the scheme of things, 1464 vs. 1492 isn't a great deal of difference so pick the bigger one. Maybe add a little headroom!

    If you want super tight buffers of exactly the right size then do something like this

    func handler(c *net.Conn) {
      maxSize:=1464
      for {
        buf := make([]byte, maxSize)
        n, _ := (*c).Read(buf)
        if n == 0 {
          continue
        }
        if n > maxSize {
          maxSize=n
        }
      }
    

    And it will adjust. NB make() is a relatively expensive call so you might like to look at sync.pool or other approaches to manage buffers

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大