You're not telling the UdpClient what IP endpoint to listen on. Even if you were to replace IPAddress.Any
with the endpoint of your network card, you'd still have the same problem.
If you want to tell the UdpClient to receive packets on a specific network card, you have to specify the IP address of that card in the constructor. Like so:
var listenEndpoint = new IPEndPoint(IPAddress.Parse("192.168.1.2"), 2000);
var myClient = new UdpClient(listenEndpoint);
Now, you may ask "What's the ref endPoint
part for when I'm calling myClient.Receive(ref endPoint)
?" That endpoint is the IP endpoint of the client. I would suggest replacing your code with something like this:
IPEndpoint clientEndpoint = null;
while (!finished)
{
var receivedBytes = myClient.Receive(ref clientEndpoint);
// clientEndpoint is no longer null - it is now populated
// with the IP address of the client that just sent you data
}
So now you have two endpoints:
listenEndpoint
, passed in through the constructor, specifying the address of the network card you want to listen on.clientEndpoint
, passed in as a ref parameter to Receive(), which will be populated with the client's IP address so you know who is talking to you.