title: C# Getting the IP Address of the client which the UDP server socket received data from
body: <p>I have a very strange problem. I'm not able to find the IP Address of the client which my server receives data from. Below is my UDP Listener class.</p>
  
  <p>The <strong>IPPacketInformation</strong> does not contain the IP. The <em>clientEndPoint</em> which I reference in my <em>EndReceiveMessageFrom</em> does neither.</p>
  
  <p>When I</p>
  
  <pre><code>Console.Writeline(((IPEndPoint)clientEndPoint).Address); 
  </code></pre>
  
  <p>I get the IP Address of the server. I have the server hosted on my own machine so I get my own IP Address. When I try to access clientEndPoint.remoteEndPoint it throws an error because the socket isn't connected (due to being UDP).</p>
  
  <p>So basically, a client from an external IP is able to send data, but I can't answer the client since I'm not able to retrieve it's IP. Any help is appreciated!</p>
  
  <pre><code>public class UdpListener
  {
      private Socket s;
      public byte[] ReceiveBuffer = new byte[2048];
  
      public UdpListener()
      {
          s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
          s.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.PacketInformation, true);
          s.Bind(new IPEndPoint(IPAddress.Any, 36200));
      }
  
      public void Listen()
      {
          try
          {
              EndPoint remoteEndPoint = new IPEndPoint(IPAddress.Any, 0);
              s.BeginReceiveMessageFrom(ReceiveBuffer, 0, ReceiveBuffer.Length, SocketFlags.None, ref remoteEndPoint, Recv, s);
          }
          catch (Exception e)
          {
              Console.WriteLine(e.ToString());
              throw;
          }
      }
  
      private void Recv(IAsyncResult res)
      {
          try
          {
              Socket receiveSocket = (Socket)res.AsyncState;
  
              EndPoint clientEndPoint = new IPEndPoint(IPAddress.Any, 0);
              IPPacketInformation packetInfo;
              SocketFlags flags = SocketFlags.None;
              int udpMessageLength = receiveSocket.EndReceiveMessageFrom(res, ref flags, ref clientEndPoint, out packetInfo);
              byte[] udpMessage = new byte[udpMessageLength];
              Array.Copy(ReceiveBuffer, udpMessage, udpMessageLength);
  
              Console.WriteLine(
                  "{0} bytes received from {1} to {2}",
                  ReceiveBuffer,
                  clientEndPoint,
                  packetInfo.Address
              );
  
              EndPoint remoteEndPoint = new IPEndPoint(IPAddress.Any, ((IPEndPoint)receiveSocket.LocalEndPoint).Port);
              s.BeginReceiveMessageFrom(ReceiveBuffer, 0, ReceiveBuffer.Length, SocketFlags.None, ref remoteEndPoint, Recv, s);
  
              //Process data
              RaiseDataReceived(new ReceivedDataArgs(packetInfo.Address, ((IPEndPoint)clientEndPoint).Port, udpMessage));
          }
          catch (Exception e)
          {
              Console.WriteLine(e.ToString());
              throw;
          }
      }
  
      public delegate void DataReceived(ReceivedDataArgs args);
  
      public event DataReceived DataReceivedEvent;
  
      private void RaiseDataReceived(ReceivedDataArgs args)
      {
          DataReceivedEvent?.Invoke(args);
      }
  }
  </code></pre>
  
  <p>I've tried to disable my firewall but that does not help. I've also done Port Forwarding on my router thus I can receive data from external clients.</p>
  
  <p><strong>EDIT</strong> to clarify the problem: </p>
  
  <p>Server hosted on my machine behind a NAT with public IP 214.214.214.214.</p>
  
  <p>The client is another machine, behind another NAT with public IP 910.910.910.910.</p>
  
  <p>The client sends a message to the server, server receives it and able to read the content. When server get the IPEndPoint of the client, the displayed IP is 214.214.214.214 (IP of the server, <strong>not the Client</strong>)</p>
  
  <p><strong>EDIT</strong> Maybe I should say that I wasn't able to receive messages from clients on external networks until I ordered a "Dynamic IP" from my ISP. Still can't get the public IP of the source.</p>
  
  <p><strong>EDIT</strong> When using Wireshark and sniff a packet sent from external client to my server I can see it's the wrong src IP as well. In the picture below the src IP is my server IP and not the IP of the client that sent the data.</p>
  
  <p><a href="https://i.stack.imgur.com/z5Iu0.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/z5Iu0.png" alt="enter image description here"></a></p>