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

Lon*_*ong 2 sockets buffer udp go

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

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

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

小智 5

查看net包代码后,看起来SetWriteBuffer对setsockopt(对于posix)进行了系统调用。GetWriteBuffer 没有类似的函数。我能想到的唯一方法是像这样对 getsockopt 进行另一个系统调用。

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()
Run Code Online (Sandbox Code Playgroud)