duanlv2788
duanlv2788
2017-04-17 11:22

golang获取udp套接字缓冲区大小

已采纳

I'm writing a udp client and set udp socket send buffer by SetWriteBuffer.

   addr, _ := net.ResolveUDPAddr("udp", ":8089")
   conn, err :=net.DialUDP("udp", nil, addr)  
   err =conn.SetWriteBuffer(64*1024*1024)

as above, how can I test set the value is effective or get the send buffer value after call SetWriteBuffer function.
Thank you all.

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

2条回答

  • duanrong6802 duanrong6802 4年前

    After looking at the net package code, it looks like SetWriteBuffer makes a syscall to setsockopt (for posix). There is no similar function for GetWriteBuffer. The only way i can think to do this is by making another syscall to getsockopt like so.

    addr, _ := net.ResolveUDPAddr("udp", ":8089")
    conn, _ := net.DialUDP("udp", nil, addr)
    conn.SetWriteBuffer(10 * 1024)
    fd, _ := conn.File()
    value, _ := syscall.GetsockoptInt(int(fd.Fd()), syscall.SOL_SOCKET, syscall.SO_SNDBUF)
    log.Println(value)
    fd.Close()
    conn.Close()
    
    点赞 评论 复制链接分享
  • duanbanzhi4419 duanbanzhi4419 4年前

    Check the error value returned by SetWriteBuffer. For example,

    package main
    
    import (
        "log"
        "net"
    )
    
    func main() {
        addr, err := net.ResolveUDPAddr("udp", ":8089")
        conn, err := net.DialUDP("udp", nil, addr)
    
        err = conn.SetWriteBuffer(64 * 1024 * 1024)
        if err != nil {
            log.Print(err)
        }
    }
    
    点赞 评论 复制链接分享

相关推荐