Search code examples
goudp

How to send UDP Packet with specify address


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)?


Solution

  • 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.