I have a computer with two network cards (attached to seperate networks) and
I am having trouble getting my C# application to send its multicast packets
on a specific one. It receives just fine (I'm assuming because its listening
to both networks), but it seems like it just picks the default to send over.
I have implemented the MulticastOption(RemoteIP, LocalIP), but it doesn't
seem to be changing the interface it sends over.
Here's some of my code:
public void setupSocket()
{
localEP = new IPEndPoint(localIP, localPort);
incomingEP = new IPEndPoint(IPAddress.Any, 0);
sock = new Socket(AddressFamily.InterNetwork, SocketType.Dgram,
ProtocolType.Udp);
sock.Bind((EndPoint)localEP);
if(multicast)
{
mcEP = new IPEndPoint(mcIP, localPort);
MulticastOption mcastOption = new MulticastOption(mcIP, localIP);
sock.SetSocketOption(SocketOptionLevel.IP,
SocketOptionName.AddMembership, mcastOption);
sock.SetSocketOption(SocketOptionLevel.IP,
SocketOptionName.MulticastTimeToLive, 2);
//sock.SetSocketOption(SocketOptionLevel.IP,
SocketOptionName.MulticastInterface, <??? What goes here, its listed as a
byte []. but what is its value!???>);
//sock.Connect(mcEP); // Doesn't work in multicast, use sendto()
}
else
{
remoteEP = new IPEndPoint(remoteIP, remotePort);
sock.Connect(remoteEP);
}
}
public void receive()
{
EndPoint inEP = (EndPoint)incomingEP;
Byte[] buffer = new Byte[1024];
int size = sock.ReceiveFrom(buffer, ref inEP);
}
public void send(Byte[] buffer)
{
// if multicast
if(mcEP != null)
{
//client.Send(buffer, buffer.Length, mcEP);
//sock.Send(buffer);
sock.SendTo(buffer, mcEP);
}
else
{
sock.Send(buffer);
}
}
In addition, it seems to send O.K. when I send unicast. It doesn't work
when I try to do sock.connect(...) while using multicast. Any help would be
much appreciated! Thanks!