Trax
Trax

Reputation: 1033

C# UdpClient Can't Send Multicast UDP Packet

I have a laptop which has a wireless adapter with ip address "192.168.5.60". This laptop will send UDP Multicast packets.

I have a desktop pc which has a network adapter ip ""192.168.5.90". I installed a software named "Multicast Tester" which joins multicast group("239.194.190.22:4000") on this desktop pc.

Problem is if i use another software which i installed from internet on laptop and send multicast udp packets to "239.194.190.22:4000" i can receive these packets in desktop pc.

If I use my program to send these packets, i can't receive multicast packets.

My code :

UdpClient udpClient = new UdpClient();
udpClient.client.bind(new IPEndPoint(IPAddress.Parse("192.168.5.60"), 0));
udpClient.JoinMulticastGroup(IPAddress.Parse("239.194.190.22"));
udpClient.send(myData, myData.length, new IPEndPoint(IPAddress.Parse("239.194.190.22"), 4000));

Note : Both computers have multiple nics.

Upvotes: 0

Views: 1407

Answers (2)

lossleader
lossleader

Reputation: 13495

From your description, the problem is that your sender is sending out its system default multicast interface which happens to be an interface not on the link with the 192.168.5/24 network. If you use IP_MULTICAST_IF with either the sender's ip or the index of the interface (as shown by ipconfig) instead of IP_MULTICAST_TTL then the TTL of 1 is fine since you are then using the shared link, i.e:

_udpClient.Client.SetSocketOption(SocketOptionLevel.IP,
  SocketOptionName.MulticastInterface, IPAddress.Parse("192.168.5.60").GetAddressBytes());

(Where you may need to do some more work on the address to make it a DWORD in network order, and based on ip options and enums)

Upvotes: 1

Trax
Trax

Reputation: 1033

I just solved it. It looks like default TTL value for UDP Multicast packets is '1'. ! I changed it to '16' by using this code:

_udpClient.Client.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.MuticastTimeToLive, 16);

Upvotes: 0

Related Questions