C# UDP Socket: Get receiver address

前端 未结 5 1662
萌比男神i
萌比男神i 2020-12-29 07:04

I have an asynchronous UDP server class with a socket bound on IPAddress.Any, and I\'d like to know which IPAddress the received packet was sent to (...or received on). It

5条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-29 07:47

    One way you'll get an address from that socket would be to connect it to the sender. Once you do that, you'll be able to get the local address (or at least, one routable to the sender), however, you'll only be able to receive messages from the connected endpoint.

    To unconnect, you'll need to use connect again, this time specifying an address with a family of AF_UNSPEC. Unfortunately, I don't know how this would be achieved in C#.

    (Disclaimer: I've never written a line of C#, this applies to Winsock in general)

提交回复
热议问题