Export (0) Print
Expand All

MulticastOption Class

Contains IPAddress values used to join and drop multicast groups.

System.Object
  System.Net.Sockets.MulticastOption

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

'Declaration
Public Class MulticastOption

The MulticastOption type exposes the following members.

  NameDescription
Public methodMulticastOption(IPAddress)Initializes a new version of the MulticastOption class for the specified IP multicast group.
Public methodMulticastOption(IPAddress, Int32)Initializes a new instance of the MulticastOption class with the specified IP multicast group address and interface index.
Public methodMulticastOption(IPAddress, IPAddress)Initializes a new instance of the MulticastOption class with the specified IP multicast group address and local IP address associated with a network interface.
Top

  NameDescription
Public propertyGroupGets or sets the IP address of a multicast group.
Public propertyInterfaceIndexGets or sets the index of the interface that is used to send and receive multicast packets.
Public propertyLocalAddressGets or sets the local address associated with a multicast group.
Top

  NameDescription
Public methodEquals(Object)Determines whether the specified object is equal to the current object. (Inherited from Object.)
Protected methodFinalizeAllows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.)
Public methodGetHashCodeServes as the default hash function. (Inherited from Object.)
Public methodGetTypeGets the Type of the current instance. (Inherited from Object.)
Protected methodMemberwiseCloneCreates a shallow copy of the current Object. (Inherited from Object.)
Public methodToStringReturns a string that represents the current object. (Inherited from Object.)
Top

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.

' This is the listener example that shows how to use the MulticastOption class.  
' In particular, it shows how to use the MulticastOption(IPAddress, IPAddress)  
' constructor, which you need to use if you have a host with more than one  
' network card. 
' The first parameter specifies the multicast group address, and the second  
' specifies the local address of the network card 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 comand  
' from the command line.  


Imports System
Imports System.Net
Imports System.Net.Sockets
Imports System.Text
Imports Microsoft.VisualBasic


Namespace Mssc.TransportProtocols.Utilities

  Module M_TestMulticastOption

    Public Class TestMulticastOption

      Private Shared mcastAddress As IPAddress
      Private Shared mcastPort As Integer 
      Private Shared mcastSocket As Socket
      Private Shared mcastOption As MulticastOption


      Private Shared Sub MulticastOptionProperties()
        Console.WriteLine(("Current multicast group is: " + mcastOption.Group.ToString()))
        Console.WriteLine(("Current multicast local address is: " + mcastOption.LocalAddress.ToString()))
      End Sub 'MulticastOptionProperties


      Private Shared Sub StartMulticast()

        Try
          mcastSocket = New Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp)

          Console.Write("Enter the local IP address: ")

          Dim localIPAddr As IPAddress = IPAddress.Parse(Console.ReadLine())

          'IPAddress localIP = IPAddress.Any; 
          Dim localEP As EndPoint = CType(New IPEndPoint(localIPAddr, mcastPort), EndPoint)

          mcastSocket.Bind(localEP)

          ' Define a MulticastOption object specifying the multicast group  
          ' address and the local IPAddress. 
          ' The multicast group address is the same as the address used by the server.
          mcastOption = New MulticastOption(mcastAddress, localIPAddr)

          mcastSocket.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.AddMembership, mcastOption)

        Catch e As Exception
          Console.WriteLine(e.ToString())
        End Try 
      End Sub 'StartMulticast


      Private Shared Sub ReceiveBroadcastMessages()
        Dim done As Boolean = False 
        Dim bytes() As Byte = New [Byte](99) {}
        Dim groupEP As New IPEndPoint(mcastAddress, mcastPort)
        Dim remoteEP As EndPoint = CType(New IPEndPoint(IPAddress.Any, 0), EndPoint)


        Try 
          While Not done
            Console.WriteLine("Waiting for multicast packets.......")
            Console.WriteLine("Enter ^C to terminate.")

            mcastSocket.ReceiveFrom(bytes, remoteEP)

            Console.WriteLine("Received broadcast from {0} :" + ControlChars.Lf + " {1}" + ControlChars.Lf, groupEP.ToString(), Encoding.ASCII.GetString(bytes, 0, bytes.Length))
          End While



          mcastSocket.Close()

        Catch e As Exception
          Console.WriteLine(e.ToString())
        End Try 
      End Sub 'ReceiveBrodcastMessages


      Public Shared Sub Main(ByVal args() As String)
        ' Initialize the 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 
        ' as the values used by the sender.
        mcastAddress = IPAddress.Parse("224.168.100.2")
        mcastPort = 11000

        ' Start a multicast group.
        StartMulticast()

        ' Display MulticastOption properties.
        MulticastOptionProperties()

        ' Receive broadcast messages.
        ReceiveBroadcastMessages()
      End Sub 'Main
    End Class 'TestMulticastOption

  End Module 

End Namespace
' This sender example must be used in conjunction with the 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 command 
' from the command line.  
'   
Imports System
Imports System.Net.Sockets
Imports System.Net
Imports System.Text
Imports Microsoft.VisualBasic

Namespace Mssc.TransportProtocols.Utilities

  Module M_TestMulticastOption

    Class TestMulticastOption

      Private Shared mcastAddress As IPAddress
      Private Shared mcastPort As Integer 
      Private Shared mcastSocket As Socket


      Shared Sub JoinMulticastGroup()
        Try 
          ' Create a 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 multicast messages. 
          Console.Write(ControlChars.Lf + "Enter local IPAddress for sending multicast packets: ")
          Dim localIPAddr As IPAddress = IPAddress.Parse(Console.ReadLine())

          ' Create an IPEndPoint object.  
          Dim IPlocal As New IPEndPoint(localIPAddr, 0)

          ' Bind this endpoint to the multicast socket.
          mcastSocket.Bind(IPlocal)

          ' Define a MulticastOption object specifying the multicast group  
          ' address and the local IP address. 
          ' The multicast group address is the same as the address used by the listener. 
          Dim mcastOption As MulticastOption
          mcastOption = New MulticastOption(mcastAddress, localIPAddr)

          mcastSocket.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.AddMembership, mcastOption)

        Catch e As Exception
          Console.WriteLine((ControlChars.Lf + e.ToString()))
        End Try 
      End Sub 'JoinMulticast


      Shared Sub BroadcastMessage(ByVal message As String)
        Dim endPoint As IPEndPoint

        Try 
          'Send multicast packets to the listener.
          endPoint = New IPEndPoint(mcastAddress, mcastPort)
          mcastSocket.SendTo(ASCIIEncoding.ASCII.GetBytes(message), endPoint)
          Console.WriteLine("Multicast data sent.....")
        Catch e As Exception
          Console.WriteLine((ControlChars.Lf + e.ToString()))
        End Try

        mcastSocket.Close()
      End Sub 'BrodcastMessage


      Public Shared Sub Main(ByVal args() As String)
        ' Initialize the 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 as the  
        ' values used by the sender.
        mcastAddress = IPAddress.Parse("224.168.100.2")
        mcastPort = 11000

        ' Join the listener multicast group.
        JoinMulticastGroup()

        ' Broadcast the message to the listener.
        BroadcastMessage("Hello multicast listener.")
      End Sub 'Main
    End Class 'TestMulticastOption

  End Module 

End Namespace

.NET Framework

Supported in: 4.5.2, 4.5.1, 4.5, 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

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

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