how to set SocketOptionName.MulticastInterface

  • Thread starter Thread starter PHLICS_Admin
  • Start date Start date
P

PHLICS_Admin

Hi All,

Following code is used to set multicast interface :

IPAddress ipLan = IPAddress.Parse("...");
int iIP = (int)(ipLan.Address) ;

objMCastSender.SetSocketOption(SocketOptionLevel.IP,SocketOptionName.Multica
stInterface , iIP) ;

When the IP address is "192.168.0.231" , Overflow error occur. but if change
the code to:

IPAddress ipLan = IPAddress.Parse("...");
long iIP = (long)(ipLan.Address) ;

objMCastSender.SetSocketOption(SocketOptionLevel.IP,SocketOptionName.Multica
stInterface , iIP) ;

Another error occur: optionerror.

Would you please give me some advise about how to solve it .

Thanks,

Phlics
 
PHLICS_Admin said:
Hi All,

Following code is used to set multicast interface :

IPAddress ipLan = IPAddress.Parse("...");
int iIP = (int)(ipLan.Address) ;

objMCastSender.SetSocketOption(SocketOptionLevel.IP,SocketOptionName.Multica
stInterface , iIP) ;

Would you please give me some advise about how to solve it .
Phlics -

You don't need to convert the IP address to an int for it to work.
You can use the GetAddressBytes() IPAddress method, and then use that
value for the SetSocketOption() method. This worked fine on my PC:

Socket sock = new Socket(AddressFamily.InterNetwork,
SocketType.Dgram,
ProtocolType.Udp);
IPAddress ipLan = IPAddress.Parse("192.168.1.10");
byte[] bIP = ipLan.GetAddressBytes();
sock.SetSocketOption(SocketOptionLevel.IP,
SocketOptionName.MulticastInterface,
bIP);
IPEndPoint iep = new IPEndPoint(IPAddress.Parse("224.100.0.1"),
9050);
byte[] data = Encoding.ASCII.GetBytes("This is a test");
sock.SendTo(data, iep);
sock.Close();

Hope this helps shed some light on your problem.

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/WileyTitle/productCd-0471433012.html
 
Thanks for your help .

Microsoft KB number 318911 say:

When you use MulticastInterface as the value for the SocketOptionName
enumeration, the optionValue expects an Index value of the particular
Network Adapter or Interface. Note that there is currently no API in the
..NET Framework to determine the index of a particular adapter. Therefore,
the user must input the index value

I wonder whether your method can work correctly.

Regards.

Phlics

Rich Blum said:
"PHLICS_Admin" <[email protected]> wrote in message
Hi All,

Following code is used to set multicast interface :

IPAddress ipLan = IPAddress.Parse("...");
int iIP = (int)(ipLan.Address) ;

objMCastSender.SetSocketOption(SocketOptionLevel.IP,SocketOptionName.Multica
stInterface , iIP) ;

Would you please give me some advise about how to solve it .
Phlics -

You don't need to convert the IP address to an int for it to work.
You can use the GetAddressBytes() IPAddress method, and then use that
value for the SetSocketOption() method. This worked fine on my PC:

Socket sock = new Socket(AddressFamily.InterNetwork,
SocketType.Dgram,
ProtocolType.Udp);
IPAddress ipLan = IPAddress.Parse("192.168.1.10");
byte[] bIP = ipLan.GetAddressBytes();
sock.SetSocketOption(SocketOptionLevel.IP,
SocketOptionName.MulticastInterface,
bIP);
IPEndPoint iep = new IPEndPoint(IPAddress.Parse("224.100.0.1"),
9050);
byte[] data = Encoding.ASCII.GetBytes("This is a test");
sock.SendTo(data, iep);
sock.Close();

Hope this helps shed some light on your problem.

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/WileyTitle/productCd-0471433012.html
 
It can work.

Thanks,

Rich Blum said:
"PHLICS_Admin" <[email protected]> wrote in message
Hi All,

Following code is used to set multicast interface :

IPAddress ipLan = IPAddress.Parse("...");
int iIP = (int)(ipLan.Address) ;

objMCastSender.SetSocketOption(SocketOptionLevel.IP,SocketOptionName.Multica
stInterface , iIP) ;

Would you please give me some advise about how to solve it .
Phlics -

You don't need to convert the IP address to an int for it to work.
You can use the GetAddressBytes() IPAddress method, and then use that
value for the SetSocketOption() method. This worked fine on my PC:

Socket sock = new Socket(AddressFamily.InterNetwork,
SocketType.Dgram,
ProtocolType.Udp);
IPAddress ipLan = IPAddress.Parse("192.168.1.10");
byte[] bIP = ipLan.GetAddressBytes();
sock.SetSocketOption(SocketOptionLevel.IP,
SocketOptionName.MulticastInterface,
bIP);
IPEndPoint iep = new IPEndPoint(IPAddress.Parse("224.100.0.1"),
9050);
byte[] data = Encoding.ASCII.GetBytes("This is a test");
sock.SendTo(data, iep);
sock.Close();

Hope this helps shed some light on your problem.

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/WileyTitle/productCd-0471433012.html
 
Back
Top