从UDP连接C#获取客户端的IP [英] Getting client ip from UDP connection C#

查看:925
本文介绍了从UDP连接C#获取客户端的IP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在有一个UDP包在端口上侦听服务器应用程序。当一个被发送到服务器,它接收它正确并处理它。有什么办法,我可以得到的数据包来自何处的IP地址?



下面是我如何创建套接字

  this.UDPListener =新的Socket(AddressFamily.InterNetwork,SocketType.Dgram,ProtocolType.Udp); 

IPEndPoint端点=新IPEndPoint(IPAddress.Any,端口);
this.UDPListener.Bind(终点);

的SocketAsyncEventArgs socketEventArgs =新的SocketAsyncEventArgs();
socketEventArgs.SetBuffer(this.ReceiveBuffer,0,this.ReceiveBuffer.Length);
socketEventArgs.Completed + =新的EventHandler<&的SocketAsyncEventArgs GT;(的onReceive);
如果(this.UDPListener.ReceiveAsync(socketEventArgs)!)
ThreadPool.QueueUserWorkItem(新WaitCallback((对象o)=> this.OnReceive(这一点,socketEventArgs)));



当的onReceive被称为没有什么,其中含有消息来自这个ip。我haved通过的SocketAsyncEventArgs看去,我看到的是监听的IP



编辑:



下面是什么我终于实现了。

  this.UDPListener =新的Socket(AddressFamily.InterNetwork,SocketType.Dgram,ProtocolType.Udp); 
this.UDPListener.Bind(新IPEndPoint(IPAddress.Any,端口));

端点remoteEndPoint =新IPEndPoint(IPAddress.Any,0);
this.UDPListener.BeginReceiveFrom(ReceiveBuffer,0,ReceiveBuffer.Length,SocketFlags.None,裁判remoteEndPoint,的onReceive,this.UDPListener);



然后在的onReceive继承人如何获得的数据和信息



  //获取收到的消息。 
插槽receiveSocket =(插座)AsyncResult.AsyncState;
端点clientEndPoint =新IPEndPoint(IPAddress.Any,0);
INT udpMessageLength = receiveSocket.EndReceiveFrom(AsyncResult,楼盘clientEndPoint);
字节[] = udpMessage新的字节[udpMessageLength]
Array.Copy(ReceiveBuffer,udpMessage,udpMessageLength);

//开始监听新的消息。
端点remoteEndPoint =新IPEndPoint(IPAddress.Any,Int32.Parse(((IPEndPoint)receiveSocket.LocalEndPoint).Port.ToString()));
this.UDPListener.BeginReceiveFrom(ReceiveBuffer,0,ReceiveBuffer.Length,SocketFlags.None,裁判remoteEndPoint,的onReceive,this.UDPListener);

//处理接收到的消息
的Debug.WriteLine(收到{0}从字节{1}:{2}到{3} {4},udpMessageLength,(( IPEndPoint)clientEndPoint)。地址,((IPEndPoint)clientEndPoint).Port,((IPEndPoint)receiveSocket.LocalEndPoint)。地址,((IPEndPoint)receiveSocket.LocalEndPoint).Port);


解决方案

我不知道 UdpClient ,但如果你直接使用插座类,你可以调用 .ReceiveFrom(字节[],参考端点)方法,并接收通过第二个参数的远程地址。


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

解决方案

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.

这篇关于从UDP连接C#获取客户端的IP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆