如何使用指定地址发送UDP数据包

huangapple go评论74阅读模式
英文:

How to send UDP Packet with specify address

问题

我有一个问题。UDP服务器监听0.0.0.0:1137,并且服务器有两个地址(addr1和addr2)。
如果UDP服务器拨号给addr1,服务器可能通过addr2将数据包发送给客户端(这取决于路由)。问题是,我如何将数据包发送给具有特定地址(与客户端拨号相同)的客户端?

英文:

I have a problem. The udp Server listen 0.0.0.0:1137, and the sever have two address(addr1 and addr2).
If the udp Server dial addr1, the server maybe send the packet to client via addr2 (it depend on route). The problem is that How can I send the packet to the client with the specific address (same as the client dialed)?

答案1

得分: 0

好的,以下是翻译好的内容:

不要绑定到0.0.0.0,创建一个单独的套接字,并分别绑定到每个接口(使用net.Interfaces()来枚举本地接口)。

然后,你将会知道数据包是从套接字的哪个接口进来的。

英文:

Well don't bind to 0.0.0.0, create a separate socket and bind to each interface individually (use net.Interfaces() to enumerate local interfaces).

Then you'll know which interface the packet came in from the socket itself.

huangapple
  • 本文由 发表于 2021年12月16日 20:41:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/70379199.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定