duanlv2788
2017-04-17 11:22
浏览 675
已采纳

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.

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

我正在编写udp客户端,并通过SetWriteBuffer设置udp套接字发送缓冲区。

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

如上所述,如何测试该值是否有效或获取发送缓冲区值 调用SetWriteBuffer函数后。
谢谢大家。

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

2条回答 默认 最新

  • duanrong6802 2017-04-18 12:55
    已采纳

    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 2017-04-17 12:41

    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)
        }
    }
    
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题