当服务器在udp:0.0.0.0:port上侦听时,如何从服务器端获取客户端的远程(目标)地址?

时间:2018-07-13 08:28:39

标签: sockets go udp

您正在使用哪个版本的Go(开始版本)?

go版本go1.9.2 linux / amd64

您做了什么?

服务器端,在udp“:11110”上侦听并打印客户端的src ip和远程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())

客户端,两个客户端分别向udp发送“ 10.16.83.185:11110”和“ 127.0.0.1:11110”:

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)

您期望看到什么?

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

您看到了什么?

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

似乎是净值:UDPConn.LocalAddr()始终返回“ 0.0.0.0:11110”,而不是“ 10.16.83.185:11110”或“ 127.0.0.1:11110”。
那么,有什么方法可以实现“期望”结果?

0 个答案:

没有答案