Getting client ip from UDP connection C#

2019-07-03 23:04发布

问题:

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);

回答1:

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.



回答2:

You need to inspect the ReceiveMessageFromPacketInfo field in the arguments.

    void OnReceive(object sender, SocketAsyncEventArgs e)
    {
        var info = e.ReceiveMessageFromPacketInfo;
        byte[] address = info.Address.GetAddressBytes();
    }


回答3:

According to http://msdn.microsoft.com/en-us/library/system.net.sockets.socketasynceventargs.remoteendpoint.aspx the SocketAsyncEventArgs contains a RemoteEndPoint property that contains the other end. Is there anything in there?



标签: c# sockets udp