2

I'm using an UdpClient to read data from a multicast group.

It's configured like this:

m_udpClientReceiver = new UdpClient();
m_receivingEndPoint = new IPEndPoint(IPAddress.Any, m_port);
m_udpClientReceiver.ExclusiveAddressUse = false;
m_udpClientReceiver.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
m_udpClientReceiver.ExclusiveAddressUse = false;
m_udpClientReceiver.Client.Bind(m_receivingEndPoint);
m_udpClientReceiver.JoinMulticastGroup(m_multicastAddress, 255);

and I read it with:

Byte[] data = m_udpClientReceiver.Receive(ref m_receivingEndPoint);

I've several network cards(two LAN, one wifi), that are bound on differents subnets. I need to know on which network card(which ip in fact) the request has been received.

How can I achieve this?

Thank you!

J4N
  • 19,480
  • 39
  • 187
  • 340
  • i think that you can read it in the packet data/header – giammin Mar 04 '13 at 14:20
  • ? Where? The header will contains the multicast address – J4N Mar 04 '13 at 14:25
  • the udp checksum si calculated with the receiver ip address so it must be somewhere – giammin Mar 04 '13 at 14:40
  • Hi, maybe this can help you http://stackoverflow.com/questions/29890/how-to-get-your-own-local-ip-address-from-an-udp-socket-c-c – Christopher Cabezudo Rodriguez Mar 04 '13 at 14:44
  • @ChristopherCabezudoRodriguez : It will not works with wifi+LAN networks on the same subnetwork. – J4N Mar 04 '13 at 15:03
  • After the call to `Receive`, `m_receivingEndPoint` will have the IP address of the remote (sending) host. i.e. you've named `m_receivingEndPoint` wrong. – Peter Ritchie Mar 04 '13 at 15:17
  • @PeterRitchie : Yeah I know, I'm already using this for another feature, but here, I don't need the remote IP, I need the local IP on which I received this packet. – J4N Mar 04 '13 at 15:23
  • There's no way to know for sure, unless you bind to a specific IP when you create the `UdpClient`. `UdpClient.Client.LocalEndPoint` would contain the local end point; but if you bound to "any", then that would be the end point. – Peter Ritchie Mar 04 '13 at 15:44

2 Answers2

0

As an alternative have you considered not joining a multicast group? You can send and receive multicast packets just as easily using the standard UDPClient class. i.e.

UdpClient.Send(byte[] dgram, int bytes, IPEndPoint endPoint)

where endPoint = new IPEndPoint(IPAddress.Broadcast, <port number>). And on the receive still using:

Byte[] data = m_udpClientReceiver.Receive(ref m_receivingEndPoint);

where m_receivingEndPoint is now correctly set? I've just tested this and it works fine.

MarcF
  • 3,169
  • 2
  • 30
  • 57
  • I'm not an expert, but I guess that if this method exists, it must do something more than the Receive method with the correct ref, no? – J4N Mar 04 '13 at 16:00
0

I'm finally using the BeginReceive method(async), and I'm giving as context the ip on which it's bound

J4N
  • 19,480
  • 39
  • 187
  • 340