This documentation is archived and is not being maintained.

MulticastOption Class

Contains IPAddress values used to join and drop multicast groups.

Namespace:  System.Net.Sockets
Assembly:  System (in System.dll)

public ref class MulticastOption

Use a MulticastOption to store the IPAddress of a multicast group you want to join or drop. Use the Socket::SetSocketOption method with the following parameters to join a multicast group.

Parameter

Value

socketOptionLevel

SocketOptionLevel::Udp

socketOptionName

AddMembership

object

MulticastOption

Use DropMembership to drop a multicast group.

The following examples join the default IP interface to an IP multicast group. They assume the IP multicast group address in the range 224.0.0.0 to 239.255.255.255.

#using <System.dll>

using namespace System;
using namespace System::Net;
using namespace System::Net::Sockets;
using namespace System::Text;

// This program shows how to use the MultiCastOption type. In particular, 
// it shows how to use the MultiCastOption(IPAddress, IPAddress) constructor, 
// You need to use this constructor, in the case of multihomed host (i.e., 
// a host with more than one network card). With the first parameter you 
// specify the multicast group address, with the second you specify the 
// local address of one of the network cards you want to use for the data 
// exchange. 
// You must run this program in conjunction with the sender program as 
// follows: 
// Open a console window and run the listener from the command line. 
// In another console window run the sender. In both cases you must specify 
// the local IPAddress to use. To obtain this address run the ipconfig from 
// the command line. 
// 
public ref class TestMulticastOption
{
private:
   static IPAddress^ mcastAddress;
   static int mcastPort;
   static Socket^ mcastSocket;
   static MulticastOption^ mcastOption;

   static void MulticastOptionProperties()
   {
      Console::WriteLine( "Current multicast group is: {0}", mcastOption->Group );
      Console::WriteLine( "Current multicast local address is: {0}", mcastOption->LocalAddress );
   }


   static void StartMulticast()
   {
      try
      {
         mcastSocket = gcnew Socket( AddressFamily::InterNetwork,SocketType::Dgram,ProtocolType::Udp );
         Console::Write( "Enter the local IP Address: " );
         IPAddress^ localIPAddr = IPAddress::Parse( Console::ReadLine() );

         //IPAddress localIP = IPAddress::Any;
         EndPoint^ localEP = dynamic_cast<EndPoint^>(gcnew IPEndPoint( localIPAddr,mcastPort ));
         mcastSocket->Bind( localEP );

         // Define a MuticastOption object specifying the multicast group 
         // address and the local IPAddress. 
         // The multicast group address is the same one used by the server.
         mcastOption = gcnew MulticastOption( mcastAddress,localIPAddr );
         mcastSocket->SetSocketOption( SocketOptionLevel::IP, SocketOptionName::AddMembership, mcastOption );

      }
      catch ( Exception^ e ) 
      {
         Console::WriteLine( e );
      }

   }

   static void ReceiveBrodcastMessages()
   {
      bool done = false;
      array<Byte>^bytes = gcnew array<Byte>(100);
      IPEndPoint^ groupEP = gcnew IPEndPoint( mcastAddress,mcastPort );
      EndPoint^ remoteEP = dynamic_cast<EndPoint^>(gcnew IPEndPoint( IPAddress::Any,0 ));
      try
      {
         while (  !done )
         {
            Console::WriteLine( "Waiting for Multicast packets......." );
            Console::WriteLine( "Enter ^C to terminate." );
            mcastSocket->ReceiveFrom( bytes, remoteEP );
            Console::WriteLine( "Received broadcast from {0} :\n {1}\n", groupEP, Encoding::ASCII->GetString( bytes, 0, bytes->Length ) );
         }
         mcastSocket->Close();
      }
      catch ( Exception^ e ) 
      {
         Console::WriteLine( e );
      }

   }


public:
   static void Main()
   {

      // Initialize multicast address group and multicast port. 
      // Both address and port are selected from the allowed sets as 
      // defined in the related RFC documents. These are the same values 
      // used by the sender.
      mcastAddress = IPAddress::Parse( "224.168.100.2" );
      mcastPort = 11000;

      // Start a multicast group.
      StartMulticast();

      // Display multicast option properties.
      MulticastOptionProperties();

      // Receive brodcast messages.
      ReceiveBrodcastMessages();
   }

};

int main()
{
   TestMulticastOption::Main();
}
#using <mscorlib.dll>
#using <System.dll>

using namespace System;
using namespace System::Net;
using namespace System::Net::Sockets;
using namespace System::Text;


// This program shows how to use the MultiCastOption type. In particular,
// it shows how to use the MultiCastOption(IPAddress, IPAddress) constructor,
// You need to use this constructor, in the case of multihomed host (i.e.,
// a host with more than one network card). With the first parameter you
// specify the multicast group address, with the second you specify the
// local address of one of the network cards you want to use for the data
// exchange.
// You must run this program in conjunction with the sender program as
// follows:
// Open a console window and run the listener from the command line.
// In another console window run the sender. In both cases you must specify
// the local IPAddress to use. To obtain this address run the ipconfig from
// the command line.
//

public __gc class TestMulticastOption {
private:
   static IPAddress*  mcastAddress;
   static int  mcastPort;
   static Socket*  mcastSocket;
   static MulticastOption*  mcastOption;

   static void MulticastOptionProperties() {
      Console::WriteLine(S"Current multicast group is: {0}", mcastOption->Group);
      Console::WriteLine(S"Current multicast local address is: {0}", mcastOption->LocalAddress);
   }

   static void StartMulticast() {

      try {
         mcastSocket = new Socket(AddressFamily::InterNetwork,
            SocketType::Dgram,
            ProtocolType::Udp);

         Console::Write(S"Enter the local IP Address: ");

         IPAddress* localIPAddr = IPAddress::Parse(Console::ReadLine());

         //IPAddress localIP = IPAddress::Any;
         EndPoint* localEP = dynamic_cast<EndPoint*>(new IPEndPoint(localIPAddr, mcastPort));

         mcastSocket->Bind(localEP);


         // Define a MuticastOption object specifying the multicast group
         // address and the local IPAddress.
         // The multicast group address is the same one used by the server.
         mcastOption = new MulticastOption(mcastAddress, localIPAddr);

         mcastSocket->SetSocketOption(SocketOptionLevel::IP,
            SocketOptionName::AddMembership,
            mcastOption);

      } catch (Exception* e) {
         Console::WriteLine(e);
      }
   }

   static void ReceiveBrodcastMessages() {
      bool done = false;
      Byte bytes[] = new Byte[100];
      IPEndPoint* groupEP = new IPEndPoint(mcastAddress, mcastPort);
      EndPoint* remoteEP = dynamic_cast<EndPoint*> (new IPEndPoint(IPAddress::Any, 0));

      try {
         while (!done) {
            Console::WriteLine(S"Waiting for Multicast packets.......");
            Console::WriteLine(S"Enter ^C to terminate.");

            mcastSocket->ReceiveFrom(bytes, &remoteEP);

            Console::WriteLine(S"Received broadcast from {0} :\n {1}\n",
               groupEP,
               Encoding::ASCII->GetString(bytes, 0, bytes->Length));
         }
         mcastSocket->Close();
      } catch (Exception* e) {
         Console::WriteLine(e);
      }
   }

public:
   static void Main() {
      // Initialize multicast address group and multicast port.
      // Both address and port are selected from the allowed sets as
      // defined in the related RFC documents. These are the same values
      // used by the sender.
      mcastAddress = IPAddress::Parse(S"224.168.100.2");
      mcastPort = 11000;

      // Start a multicast group.
      StartMulticast();

      // Display multicast option properties.
      MulticastOptionProperties();

      // Receive brodcast messages.
      ReceiveBrodcastMessages();
   }
};

int main()
{
   TestMulticastOption::Main();
}
#using <System.dll>

using namespace System;
using namespace System::Net::Sockets;
using namespace System::Net;
using namespace System::Text;

// This is an auxiliary program to be used in conjunction with a listener 
// program. 
// You must run this program as follows: 
// Open a console window and run the listener from the command line. 
// In another console window run the sender. In both cases you must specify 
// the local IPAddress to use. To obtain this address run the ipconfig 
// from the command line. 
// 
ref class TestMulticastOption
{
private:
   static IPAddress^ mcastAddress;
   static int mcastPort;
   static Socket^ mcastSocket;
   static void JoinMulticast()
   {
      try
      {

         // Create multicast socket.
         mcastSocket = gcnew Socket( AddressFamily::InterNetwork,SocketType::Dgram,ProtocolType::Udp );

         // Get the local IP address used by the listener and the sender to 
         // exchange data in a multicast fashion.
         Console::Write( "\nEnter local IPAddress for sending multicast packets: " );
         IPAddress^ localIPAddr = IPAddress::Parse( Console::ReadLine() );

         // Create an IPEndPoint Object*.
         IPEndPoint^ IPlocal = gcnew IPEndPoint( localIPAddr,0 );

         // Bind this end point to the multicast socket.
         mcastSocket->Bind( IPlocal );

         // Define a MuticastOption Object* specifying the multicast group 
         // address and the local IPAddress. 
         // The multicast group address is the same one used by the listener.
         MulticastOption^ mcastOption;
         mcastOption = gcnew MulticastOption( mcastAddress,localIPAddr );
         mcastSocket->SetSocketOption( SocketOptionLevel::IP, SocketOptionName::AddMembership, mcastOption );
      }
      catch ( Exception^ e ) 
      {
         Console::WriteLine( "\n {0}", e );
      }

   }

   static void BrodcastMessage( String^ message )
   {
      IPEndPoint^ endPoint;
      try
      {

         //Send multicast packets to the listener.
         endPoint = gcnew IPEndPoint( mcastAddress,mcastPort );
         mcastSocket->SendTo( ASCIIEncoding::ASCII->GetBytes( message ), endPoint );
         Console::WriteLine( "Multicast data sent....." );
      }
      catch ( Exception^ e ) 
      {
         Console::WriteLine( "\n {0}", e );
      }

      mcastSocket->Close();
   }


public:
   static void main()
   {

      // Initialize multicast address group and multicast port. 
      // Both address and port are selected from the allowed sets as 
      // defined in the related RFC documents. These are the same values 
      // used by the sender.
      mcastAddress = IPAddress::Parse( "224.168.100.2" );
      mcastPort = 11000;

      // Join the listener multicast group.
      JoinMulticast();

      // Broadcast message to the listener.
      BrodcastMessage( "Hello multicast listener." );
   }

};

int main()
{
   TestMulticastOption::main();
}
#using <mscorlib.dll>
#using <System.dll>

using namespace System;
using namespace System::Net::Sockets;
using namespace System::Net;
using namespace System::Text;

// This is an auxiliary program to be used in conjunction with a listener
// program.
// You must run this program as follows:
// Open a console window and run the listener from the command line.
// In another console window run the sender. In both cases you must specify
// the local IPAddress to use. To obtain this address run the ipconfig
// from the command line.
//

__gc class TestMulticastOption {

   static IPAddress* mcastAddress;
   static int mcastPort;
   static Socket* mcastSocket;

   static void JoinMulticast() {
      try {
         // Create multicast socket.
         mcastSocket = new Socket(AddressFamily::InterNetwork,
            SocketType::Dgram,
            ProtocolType::Udp);

         // Get the local IP address used by the listener and the sender to
         // exchange data in a multicast fashion.
         Console::Write(S"\nEnter local IPAddress for sending multicast packets: ");
         IPAddress*  localIPAddr = IPAddress::Parse(Console::ReadLine());

         // Create an IPEndPoint Object*.
         IPEndPoint* IPlocal = new IPEndPoint(localIPAddr, 0);

         // Bind this end point to the multicast socket.
         mcastSocket->Bind(IPlocal);

         // Define a MuticastOption Object* specifying the multicast group
         // address and the local IPAddress.
         // The multicast group address is the same one used by the listener.
         MulticastOption* mcastOption;
         mcastOption = new MulticastOption(mcastAddress, localIPAddr);

         mcastSocket->SetSocketOption(SocketOptionLevel::IP,
            SocketOptionName::AddMembership,
            mcastOption);

      } catch (Exception* e) {
         Console::WriteLine(S"\n {0}", e);
      }
   }

   static void BrodcastMessage(String* message) {
      IPEndPoint* endPoint;

      try {
         //Send multicast packets to the listener.
         endPoint = new IPEndPoint(mcastAddress, mcastPort);
         mcastSocket->SendTo(ASCIIEncoding::ASCII->GetBytes(message), endPoint);
         Console::WriteLine(S"Multicast data sent.....");
      } catch (Exception* e) {
         Console::WriteLine(S"\n {0}", e);
      }

      mcastSocket->Close();
   }

public:
   static void main() {
      // Initialize multicast address group and multicast port.
      // Both address and port are selected from the allowed sets as
      // defined in the related RFC documents. These are the same values
      // used by the sender.
      mcastAddress = IPAddress::Parse(S"224.168.100.2");
      mcastPort = 11000;

      // Join the listener multicast group.
      JoinMulticast();

      // Broadcast message to the listener.
      BrodcastMessage(S"Hello multicast listener.");
   }
};

int main()
{
   TestMulticastOption::main();
}

System::Object
  System.Net.Sockets::MulticastOption

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 3.5, 2.0, 1.0
Show: