1

What version of Go are you using (go version)?

go version go1.9.2 linux/amd64

What did you do?

Server side, listening on udp ":11110" and print client's src ip and remote ip:

ServerAddr,err := net.ResolveUDPAddr("udp",":11110")
ServerConn, err := net.ListenUDP("udp", ServerAddr)
n,addr,err := ServerConn.ReadFromUDP(buf)
fmt.Println("Received ",string(buf[0:n]), " from ",addr, " to ", ServerConn.LocalAddr())

Client side, two clients send udp to "10.16.83.185:11110" and "127.0.0.1:11110" respectively:

ServerAddr1,err := net.ResolveUDPAddr("udp", "10.16.83.185:11110")
Conn1, err := net.DialUDP("udp", nil, ServerAddr1)
_,err := Conn1.Write(buf)

ServerAddr2,err := net.ResolveUDPAddr("udp", "127.0.0.1:11110")
Conn2, err := net.DialUDP("udp", nil, ServerAddr2)
_,err := Conn2.Write(buf)

What did you expect to see?

Received  1  from  10.16.83.185:51386  to  10.16.83.185:11110
Received  1  from  127.0.0.1:58306  to  127.0.0.1:11110

What did you see instead?

Received  1  from  10.16.83.185:51386  to  0.0.0.0:11110
Received  1  from  127.0.0.1:58306  to  0.0.0.0:11110

It seems net: UDPConn.LocalAddr() would always return "0.0.0.0:11110" instead of "10.16.83.185:11110" or "127.0.0.1:11110".
So is there any way i can achieve the "expect" result?

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
woniuxu
  • 11
  • 1
  • See here for the underlying details: https://stackoverflow.com/questions/5281409/get-destination-address-of-a-received-udp-packet -- I think you can do this via golang.org/x/net/ipv4 and golang.org/x/net/ipv6, but I'll have to defer to someone else to get the time to read through the code. – JimB Jul 13 '18 at 14:36
  • @JimB With the extention "golang.org/x/net/ipv4", I obtained the expect result through PakcetConn.SetControlMessage(ipv4.FlagDst, true) and PacketConn.ReadFrom(). – woniuxu Jul 17 '18 at 06:22

0 Answers0