When you create your MulticastOption object for joining the group be sure to
use the MulticastOption(groupIP, localIP) constructor. The
MulticastOption(groupIP) constructor won't work with multi-NIC machines. If
you also want to implement a listener, each multicast listener is bound to
one specific NIC on your machine so you will need to create a listener for
each NIC.
-Ron
Here's some C# sample code:
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
public class UDPMulticastListener
{
private static readonly IPAddress GroupAddress =
IPAddress.Parse("224.168.100.2");
private const int GroupPort = 11000;
private static void StartListener()
{
bool done = false;
byte[] bytes = new Byte[100];
IPEndPoint groupEP = new IPEndPoint(GroupAddress,GroupPort);
EndPoint remoteEP = (EndPoint) new IPEndPoint(IPAddress.Any,0);
Socket listener = new Socket(AddressFamily.InterNetwork,SocketType.Dgram ,
ProtocolType.Udp);
Console.Write("Enter the IP Address to bind to : ");
IPAddress localIP = IPAddress.Parse(Console.ReadLine());
EndPoint localEP = (EndPoint)new IPEndPoint(localIP, GroupPort);
try
{
listener.Bind(localEP);
listener.SetSocketOption(SocketOptionLevel.IP,
SocketOptionName.AddMembership,
new MulticastOption(GroupAddress, localIP));
while (!done)
{
Console.WriteLine("Waiting for Multicast packets.......");
listener.ReceiveFrom(bytes, ref remoteEP);
Console.WriteLine("Received broadcast from {0} :\n {1}\n",
groupEP.ToString(),
Encoding.ASCII.GetString(bytes,0,bytes.Length));
}
listener.Close();
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
}
public static int Main(String[] args)
{
StartListener();
return 0;
}
"Rich Blum" <ri*******@juno.com> wrote in message
news:cc**************************@posting.google.c om...
"0k" <0k@0k.com> wrote in message
news:<E%*******************@news1.tin.it>...
Hi everyone, I am trying to write a small app that sends multicast udp
packets using a socket object.
I have more than one NIC on my PC and the following code works OK only
if I disable all the NICs but the one i want to use.
Of course i tried to use Socket.Bind method, but even if i use it to
bind to the correct NIC (I also verify using LocalEndPoint and IT IS the correct
one), the packet is sent on the wrong NIC! (I have a software firewall
that tells me the source NIC). What is really weird is that after executing
the first setsocketoption the program broadcasts to 224.0.0.22, WHY??? The
group is 224.5.6.7!
Can someone help me, i'm going nuts on this problem!
The packet sent to 224.0.0.22 is an IGMP V3 packet that is trying
to tell any routers around that you want to join the multicast group.
This is automatically sent by the OS when you set the AddMembership
socket option. If all you need to do is send multicast packets, you do
not need to join the multicast group. That is only used for if you
want to receive multicast packets on the socket.
As far as your problem goes, I do not know why it is not working
properly (assuming you are binding the socket to the correct IP
address for the NIC you want to use). Instead of using the Connect()
method, try using the SendTo() method along with the multicast IP
address: UdpSender.SendTo(bytes, groupEP)
When you talk about the software firewall, is that running on this
PC, or a remote PC? Internal firewall software is well known for
causing network programming problems. You might try disabling it and
testing your program. Alternatively, try loading a packet sniffer
program such as the free WinPcap drivers and the Analyzer program
(http://analyzer.polito.it) and see what is says about the packets.
Hope this gives you some ideas to work with. Good luck.
Rich Blum - Author
"C# Network Programming" (Sybex)
http://www.sybex.com/sybexbooks.nsf/Booklist/4176
"Network Performance Open Source Toolkit" (Wiley)
http://www.wiley.com/WileyCDA/WileyT...471433012.html