简体   繁体   English

如何使用UDPClient类从UDP数据包中检测目标IP地址

[英]How to detect destination IP address from UDP packets using UDPClient Class

I am working on the application which send and receive messages on UDP between client app and server app. 我正在研究在客户端应用程序和服务器应用程序之间通过UDP发送和接收消息的应用程序。

On my server I have 4 different network cards, eg nic1 = 169.524.15.12, nic2 = 169.524.15.65, etc. My DNS is point to nic2. 在我的服务器上,我有4个不同的网卡,例如nic1 = 169.524.15.12,nic2 = 169.524.15.65,等等。我的DNS指向nic2。 The client app resolves the DNS and send data to nic2. 客户端应用程序解析DNS并将数据发送到nic2。 However my server app sometime respond to client from nic1. 但是我的服务器应用有时会响应nic1的客户端。

I'm using an UdpClient to listen for incoming packets. 我正在使用UdpClient侦听传入的数据包。

This is my server application code: 这是我的服务器应用程序代码:

objSocketServer = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
EndPoint objEndPointOfServer =   new IPEndPoint(IPAddress.Any, 5000);
objSocketServer.Bind(objEndPointOfServer);
objSocketServer.BeginReceiveFrom(_arrReceivedDataBuffer, 0, BUFSIZE - 1, SocketFlags.None, ref objEndPointOfServer, DoReceiveFromClient, objSocketServer);

private void DoReceiveFromClient(IAsyncResult objIAsyncResult)
{
        try
        {
             // Get the received message.
            _objSocketReceivedClient = (Socket)objIAsyncResult.AsyncState;
            EndPoint objEndPointReceivedClient = new IPEndPoint(IPAddress.Any, 0);

            // Received data.
            int intMsgLen = _objSocketReceivedClient.EndReceiveFrom(objIAsyncResult, ref objEndPointReceivedClient);
            byte[] arrReceivedMsg = new byte[intMsgLen];
            Array.Copy(_arrReceivedDataBuffer, arrReceivedMsg, intMsgLen);

            // Client port.
            // Get and store port allocated to server1 while making request from client to server.
            int _intClientServer1Port = ((IPEndPoint)objEndPointReceivedClient).Port;

            // Send external ip and external port back to client.
            String strMessage = ((IPEndPoint)objEndPointReceivedClient).Address.ToString() + ":" + _intClientServer1Port.ToString();
            byte[] arrData = Encoding.ASCII.GetBytes(strMessage);
            objSocketServer.SendTo(arrData, arrData.Length, SocketFlags.None, objEndPointReceivedClient);

            // Start listening for a new message.
            EndPoint objEndPointNewReceivedClient = new IPEndPoint(IPAddress.Any, 0);
            objSocketServer.BeginReceiveFrom(_arrReceivedDataBuffer, 0, _arrReceivedDataBuffer.Length, SocketFlags.None, ref objEndPointNewReceivedClient, DoReceiveFromClient, objSocketServer)
        }
        catch (SocketException sx)
        {
                objSocketServer.Shutdown(SocketShutdown.Both);
                objSocketServer.Close();
        }
     }
}

Is there any way so that in code, I can detect that I have received packet on which IP address on server and revert with the response with same IP? 有什么办法可以使我在代码中检测到我已收到服务器上哪个IP地址的数据包,并使用相同IP的响应进行还原?

Arguably, I could resolve DNS in server app as well and make sure that my server app only listen to IP on which client app is sending packets, however that approach will not work for me when my server app have to listen on > 1 IP. 可以说,我也可以在服务器应用程序中解析DNS,并确保服务器应用程序仅侦听客户端应用程序正在发送数据包的IP,但是当我的服务器应用程序必须侦听大于1个IP时,这种方法对我不起作用。

The SendTo command will use the appropriate NIC (aka, local interface) for the destination address provided. SendTo命令将为提供的目标地址使用适当的NIC(又名本地接口)。 The system metrics determine that. 系统指标确定了这一点。 It's not something you set in your code. 这不是您在代码中设置的内容。 To view the system metrics, run the command netstat -rn and look at the Interface column. 要查看系统指标,请运行命令netstat -rn并查看“接口”列。 You many need to adjust those if you have a tie. 如果有平局,很多人需要调整。 You can enumerate them in code as well using GetAllNetworkInterfaces() and bind to a specific one (if that is what you wanted). 您也可以使用GetAllNetworkInterfaces()在代码中枚举它们,并将其绑定到特定的对象(如果您想要的话)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM