title: Acquiring client's IP using IPAddress.Any
body: <p>I am currently working on a simple udp client/server system, and I stumbled upon the following: When I try to get the IP of IPEndPoint (which I fetched using IPAdress.Any)in a program using UdpClient it works and I get the following result (the top one):</p>
  
  <p><img src="https://ibin.co/2R5jmYd3aJ58" alt="image"></p>
  
  <p>But when I use a normal socket instead of a UdpClient it somehow fails to distinguish the clients/IPs (bottom one). The code for both is listed below. The reason I would like to use sockets is because using the same class for both sending and receiving (not the same instance) is convenient and makes the code far more understandable.</p>
  
  <p>First</p>
  
  <pre><code>bool messageReceived = false;
  bool done = false;
  const int listenPort = 11000;
  UdpClient listener = new UdpClient(listenPort);
  IPEndPoint receiveEP = new IPEndPoint(IPAddress.Any, listenPort);
  string[] adresses = new string[2];
  public void ReceiveCallback(IAsyncResult ar)
  {
      int id = 0;
      Byte[] receiveBytes = listener.EndReceive(ar, ref receiveEP);
      for (int i = 0; i &lt; adresses.Length; i++)
      {
          if (adresses[i] == receiveEP.Address.ToString())
          {
              id = i;
              break;
          }
          else if (adresses[i] == null)
          {
              id = i;
              adresses[i] = receiveEP.Address.ToString();
              break;
          }
      }
      byte[] a= Encoding.ASCII.GetBytes("Is anybody there?");
      listener.Send(a, a.Length, receiveEP);
      Console.WriteLine("Received message from {0} (client {1}):",    adresses[id],id);
      string receiveString = Encoding.ASCII.GetString(receiveBytes);
      if (receiveString == "stop")
      {
          done = true;
      }
      Console.WriteLine("Received: {0}", receiveString);
      messageReceived = true;
  }
  public void ReceiveMessages()
  {
      Console.WriteLine("listening for messages");
      while(!done){
          try
          {
              messageReceived = false;
              if (!messageReceived)
              {
                  listener.BeginReceive(new AsyncCallback(ReceiveCallback),  null);
              }
              while (!messageReceived)
              {
              }
  
          }
          catch (Exception e)
          {
              Console.WriteLine(e.ToString());
          }
      }
      Console.WriteLine("Done receiving messages...");
      for (int i = 0; i &lt; adresses.Length; i++)
      {
          if (adresses[i] != null)
          {
              Console.WriteLine(adresses[i]);
          }
      }
      Console.ReadLine();
      listener.Close();
  }'
  </code></pre>
  
  <p>Second</p>
  
  <pre><code>bool messageReceived = false;
  bool done = false;
  const int listenPort = 11000;
  Socket listener = new Socket(AddressFamily.InterNetwork, SocketType.Dgram,  ProtocolType.Udp);
  IPEndPoint receiveEP = new IPEndPoint(IPAddress.Any, listenPort);
  string[] adresses = new string[2];
  byte[] buffer = new byte[1024];
  public void ReceiveMessages()
  {
      listener.Bind(receiveEP);
      Console.WriteLine("listening for messages");
      while (!done)
      {
          try
          {
              messageReceived = false;
              if (!messageReceived)
              {
                  listener.BeginReceive(buffer, 0,1024,SocketFlags.None,new AsyncCallback(ReceiveCallback), null);
              }
              while (!messageReceived)
              {
              }
  
          }
          catch (Exception e)
          {
              Console.WriteLine(e.ToString());
          }
      }
      Console.WriteLine("Done receiving messages...");
      for (int i = 0; i &lt; adresses.Length; i++)
      {
          if (adresses[i] != null)
          {
              Console.WriteLine(adresses[i]);
          }
      }
      Console.ReadLine();
      listener.Close();
  }
  public void ReceiveCallback(IAsyncResult ar)
  {
      int id = 0;
      int read = listener.EndReceive(ar);
      for (int i = 0; i &lt; adresses.Length; i++)
      {
          if (adresses[i] == receiveEP.Address.ToString())
          {
              id = i;
              break;
          }
          else if (adresses[i] == null)
          {
              id = i;
              adresses[i] = receiveEP.Address.ToString();
              break;
          }
      }
      Console.WriteLine("Received message from {0} (client {1}):", adresses[id], id);
      string receiveString = Encoding.ASCII.GetString(buffer,0,read);
      if (receiveString == "stop")
      {
          done = true;
      }
      Console.WriteLine("Received: {0}", receiveString);
      messageReceived = true;
  }'
  </code></pre>
  
  <p>I've already tried Socket.ReceiveMessageFrom() and using the packetinfo it returned but I ended up with the ip4 of the server even when I send from another machine. Could someone help me out?</p>