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

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 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()
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?