Multicast on only one of two networks

G

Guest

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!
 
S

Steven Cheng[MSFT]

Hi teeks,
. Regarding on the issue, I am
finding proper resource to assist you and we will update as soon as posible.

Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security(This posting is provided "AS IS",
with no warranties, and confers no rights.)
 
K

Kevin Yu [MSFT]

Hi,

First of all, I would like to confirm my understanding of your issue. From
your description, I understand that you are having trouble to send data
over a particular network adapter. If there is any misunderstanding, please
feel free to let me know.

Based on my research, this issue might be caused by incorrect binding to
network adapters. When a Socket is binded to an IP address of Any or 0 as
value, it will select the network adapter automatically. By default, it
chooses the default one to send over. That might be the point you're
suffering from.

So please check localEP objected to see the value passed in. It shouldn't
be 0 or something like 127.0.0.1.

If anything is unclear, please feel free to reply to the post.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."
 
G

Guest

You are correct. As far as I can tell the problem is in sending data over a
particular network adapter.

I have verified that the localIP that I start with is correct for the
particular network I wish to use, (the Address and m_Address values are both
0x14B3E05). This continues when I create the localEP. It also continues
when I cast it to an EndPoint and bind to the sock
(sock.LocalEndPoint.Adddress and sock.m_LocalEndPoint.Address, etc).

Nothing much has changed in all this.

Do I need to do anythign with the SetSocketOption(... ,
SocketOptionName.MulticastInterface, ...). I can't figure out what the third
argument should be if I do need it. Thanks!
 
K

Kevin Yu [MSFT]

Hi,

If you're trying to Multicast, I think you have to use SetSocketOption
method. Please check the following link for more information.

http://support.microsoft.com/?id=318911

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."
 
G

Guest

Yeah, I've seen that example. I don't know what index is though. All I can
find out is that it's a byte array. Is it a string like "1.75.81.5" or is it
index[0] = 1, index[1] = 75, index[2] = 81, index[3] = 5, or is it something
like "Local Area Connection 2"???

Thanks for your help so far :)
 
K

Kevin Yu [MSFT]

Hi,

Since SetSocketOption method has several overloads, the optionValue can be
an int, a byte array or an object like MulticastOption object. It can also
be a value from converted by IPAddress.HostToNetworkOrder and depends on
the option you need to set. But there is no overload which accepts a string.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top