Getting client ip from UDP connection C#

11,435

I don't know about UdpClient, but if you use the Socket class directly, you can call the .ReceiveFrom(byte[], ref EndPoint) method, and receive the remote address via the second argument.

Share:
11,435
thecaptain0220
Author by

thecaptain0220

I'm a professional software developer that spends most of my time in Silverlight and WPF.

Updated on June 04, 2022

Comments

  • thecaptain0220
    thecaptain0220 almost 2 years

    I currently have a server application that is listening on a port for UDP packets. When one is sent to the server, it receives it properly and processes it. Is there any way I can get the ip address of where the packet came from?

    Here is how I create the socket

    this.UDPListener = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
    
    IPEndPoint endPoint = new IPEndPoint(IPAddress.Any, Port);
    this.UDPListener.Bind(endPoint);
    
    SocketAsyncEventArgs socketEventArgs = new SocketAsyncEventArgs(); 
    socketEventArgs.SetBuffer(this.ReceiveBuffer, 0, this.ReceiveBuffer.Length);
    socketEventArgs.Completed += new EventHandler<SocketAsyncEventArgs>(OnReceive);
    if (!this.UDPListener.ReceiveAsync(socketEventArgs))
        ThreadPool.QueueUserWorkItem(new WaitCallback((Object o) => this.OnReceive(this, socketEventArgs)));
    

    When the OnReceive is called there is nothing that contains the ip where the message came from. I haved looked through the SocketAsyncEventArgs and all I see is the listening ip.

    Edit:

    Here is what I ended up doing.

    this.UDPListener = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
    this.UDPListener.Bind(new IPEndPoint(IPAddress.Any, Port));
    
    EndPoint remoteEndPoint = new IPEndPoint(IPAddress.Any, 0);
    this.UDPListener.BeginReceiveFrom(ReceiveBuffer, 0, ReceiveBuffer.Length, SocketFlags.None, ref remoteEndPoint, OnReceive, this.UDPListener);
    

    Then in the OnReceive heres how to get the data and info

    //Get the received message.
    Socket receiveSocket = (Socket)AsyncResult.AsyncState;
    EndPoint clientEndPoint = new IPEndPoint(IPAddress.Any, 0);
    int udpMessageLength = receiveSocket.EndReceiveFrom(AsyncResult, ref clientEndPoint);
    byte[] udpMessage = new byte[udpMessageLength];
    Array.Copy(ReceiveBuffer, udpMessage, udpMessageLength);
    
    //Start listening for a new message.
    EndPoint remoteEndPoint = new IPEndPoint(IPAddress.Any, Int32.Parse(((IPEndPoint)receiveSocket.LocalEndPoint).Port.ToString()));
    this.UDPListener.BeginReceiveFrom(ReceiveBuffer, 0, ReceiveBuffer.Length, SocketFlags.None, ref remoteEndPoint, OnReceive, this.UDPListener);
    
    //Handle the received message
    Debug.WriteLine("Recieved {0} bytes from {1}:{2} to {3}:{4}", udpMessageLength, ((IPEndPoint)clientEndPoint).Address, ((IPEndPoint)clientEndPoint).Port, ((IPEndPoint)receiveSocket.LocalEndPoint).Address, ((IPEndPoint)receiveSocket.LocalEndPoint).Port);
    
  • thecaptain0220
    thecaptain0220 over 13 years
    info.Address is null for me for some reason
  • Greg Buehler
    Greg Buehler over 13 years
    strange; what is Address null if you replace e.ReceiveMessageFromPacketInfo with (IPEndPoint)e.RemoteEndPoint?
  • thecaptain0220
    thecaptain0220 over 13 years
    e.RemoteEndPoint is null too. i wasn't sure if you just couldn't do it with UDP or what. it seems like that info should be there somewhere
  • thecaptain0220
    thecaptain0220 over 13 years
    Nope, null too. This is where I started looking, not sure why its not coming across.
  • Greg Buehler
    Greg Buehler over 13 years