如何确定在C#中的多播数据包的源IP? [英] How do I determine the source IP of a multicast packet in C#?

查看:232
本文介绍了如何确定在C#中的多播数据包的源IP?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要确定已送了我一个多播包,这样我可以通过单播响应它的机器的IP地址。

I need to determine the IP of a machine that has sent me a multicast packet, so that I can respond to it via unicast.

我使用的以下CSHARP(.NET 3.5)代码通过多播连接接收数据包(代码已经被编辑为简洁起见,错误检查和无关选项去掉):

I'm using the following csharp (.Net 3.5) code to receive the packets over a multicast connection (code has been edited for brevity, with error checking and irrelevant options removed):

IPEndPoint LocalHostIPEnd = new IPEndPoint(IPAddress.Any, 8623);
Socket UDPSocket = new Socket(AddressFamily.InterNetwork,SocketType.Dgram,ProtocolType.Udp); 
UDPSocket.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.MulticastLoopback, 1);

UDPSocket.Bind(LocalHostIPEnd);

//Join the multicast group
UDPSocket.SetSocketOption(
     SocketOptionLevel.IP,
     SocketOptionName.AddMembership,
     new MulticastOption(IPAddress.Parse("225.2.2.6")));

IPEndPoint LocalIPEndPoint = new IPEndPoint(IPAddress.Any ,Target_Port); 
EndPoint LocalEndPoint = (EndPoint)LocalIPEndPoint; 

// Create the state object. 
StateObject state = new StateObject();
state.buffer.Initialize();
state.workSocket = UDPSocket;
state.id = "state0";
//Set up my callback
UDPSocket.BeginReceiveMessageFrom(
    state.buffer, 
    0,
    StateObject.BufferSize, 
    0,
    ref LocalEndPoint, 
    new AsyncCallback(ReceiveCallback), 
    state);

和这里的回调,在那里我试图获取源IP:

And here's the callback, where I'm trying to get the source IP:

private void ReceiveCallback( IAsyncResult ar ) 
{
    IPEndPoint LocalIPEndPoint = new IPEndPoint(IPAddress.Any, Target_Port);
    EndPoint LocalEndPoint = (EndPoint)LocalIPEndPoint;

    // Read data from the remote device. 
    // The following code attempts to determine the IP of the sender,
    // but instead gets the IP of the multicast group.
    SocketFlags sFlags = new SocketFlags();
    IPPacketInformation ipInf = new IPPacketInformation();

    int bytesRead = client.EndReceiveMessageFrom(ar, ref sFlags,
        ref LocalEndPoint, out ipInf);

    log.Warn("Got address: " + ipInf.Address.ToString());
}



我知道正确的源IP是在IP报头,因为我可以明确看到它那里,当我在Wireshark的嗅探数据包。然而,而不是打印出发送系统的IP(192.168.3.4),上面的代码打印出来,我订阅了(225.2.2.6)的组播组的IP地址。有没有办法让源IP呢?

I know the correct source IP is in the IP header, since I can clearly see it there when I sniff the packet in wireshark. However, instead of printing out the IP of the sending system (192.168.3.4), the above code prints out the IP of the multicast group that I am subscribed to (225.2.2.6). Is there a way to get the source IP instead?

推荐答案

是不是在LocalEndPoint可变你的答案,这是端点的数据包的源,也就是说,在另一端的纨绔子弟。请注意,我可能会重命名该变量类似remoteEP,并将其初始化,以非特异性,以避免混乱的东西。

Isn't your answer in the LocalEndPoint variable, which is the EndPoint of the packet's source, i.e., the dude on the other end. Note that I would probably rename this variable something like "remoteEP", and initialize it to something non-specific to avoid confusion.

这篇关于如何确定在C#中的多播数据包的源IP?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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