Dibaca dalam bahasa Inggeris Edit

Kongsi melalui


MulticastOption Class

Definition

Contains IPAddress values used to join and drop multicast groups.

public class MulticastOption
Inheritance
MulticastOption

Examples

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;
using System.Net;
using System.Net.Sockets;
using System.Text;

// 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 command
// from the command line.
//
namespace Mssc.TransportProtocols.Utilities
{
    public class TestMulticastOption
    {
        private static IPAddress s_mcastAddress;
        private static int s_mcastPort;
        private static Socket s_mcastSocket;
        private static MulticastOption s_mcastOption;

        private static void MulticastOptionProperties()
        {
            Console.WriteLine("Current multicast group is: " + s_mcastOption.Group);
            Console.WriteLine("Current multicast local address is: " + s_mcastOption.LocalAddress);
        }

        private static void StartMulticast()
        {
            try
            {
                s_mcastSocket = new 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 = (EndPoint)new IPEndPoint(localIPAddr, s_mcastPort);

                s_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.
                s_mcastOption = new MulticastOption(s_mcastAddress, localIPAddr);

                s_mcastSocket.SetSocketOption(SocketOptionLevel.IP,
                                            SocketOptionName.AddMembership,
                                            s_mcastOption);
            }

            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
            }
        }

        private static void ReceiveBroadcastMessages()
        {
            bool done = false;
            byte[] bytes = new byte[100];
            IPEndPoint groupEP = new(s_mcastAddress, s_mcastPort);
            EndPoint remoteEP = (EndPoint)new IPEndPoint(IPAddress.Any, 0);

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

                    s_mcastSocket.ReceiveFrom(bytes, ref remoteEP);

                    Console.WriteLine("Received broadcast from {0} :\n {1}\n",
                      groupEP.ToString(),
                      Encoding.ASCII.GetString(bytes, 0, bytes.Length));
                }

                s_mcastSocket.Close();
            }

            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
            }
        }

        public static void Main(string[] args)
        {
            // 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.
            s_mcastAddress = IPAddress.Parse("224.168.100.2");
            s_mcastPort = 11000;

            // Start a multicast group.
            StartMulticast();

            // Display MulticastOption properties.
            MulticastOptionProperties();

            // Receive broadcast messages.
            ReceiveBroadcastMessages();
        }
    }
}
using System;
using System.Net.Sockets;
using System.Net;
using System.Text;

// 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.
//
namespace Mssc.TransportProtocols.Utilities
{
    class TestMulticastOption2
    {
        static IPAddress s_mcastAddress;
        static int s_mcastPort;
        static Socket s_mcastSocket;

        static void JoinMulticastGroup()
        {
            try
            {
                // Create a multicast socket.
                s_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("\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 endpoint to the multicast socket.
                s_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.
                MulticastOption mcastOption;
                mcastOption = new MulticastOption(s_mcastAddress, localIPAddr);

                s_mcastSocket.SetSocketOption(SocketOptionLevel.IP,
                                            SocketOptionName.AddMembership,
                                            mcastOption);
            }
            catch (Exception e)
            {
                Console.WriteLine("\n" + e.ToString());
            }
        }

        static void BroadcastMessage(string message)
        {
            IPEndPoint endPoint;

            try
            {
                //Send multicast packets to the listener.
                endPoint = new IPEndPoint(s_mcastAddress, s_mcastPort);
                s_mcastSocket.SendTo(ASCIIEncoding.ASCII.GetBytes(message), endPoint);
                Console.WriteLine("Multicast data sent.....");
            }
            catch (Exception e)
            {
                Console.WriteLine("\n" + e.ToString());
            }

            s_mcastSocket.Close();
        }

        static void Main(string[] args)
        {
            // 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.
            s_mcastAddress = IPAddress.Parse("224.168.100.2");
            s_mcastPort = 11000;

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

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

Remarks

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.

Constructors

MulticastOption(IPAddress, Int32)

Initializes a new instance of the MulticastOption class with the specified IP multicast group address and interface index.

MulticastOption(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.

MulticastOption(IPAddress)

Initializes a new version of the MulticastOption class for the specified IP multicast group.

Properties

Group

Gets or sets the IP address of a multicast group.

InterfaceIndex

Gets or sets the index of the interface that's used to send and receive multicast packets.

LocalAddress

Gets or sets the local address associated with a multicast group.

Methods

Equals(Object)

Determines whether the specified object is equal to the current object.

(Inherited from Object)
GetHashCode()

Serves as the default hash function.

(Inherited from Object)
GetType()

Gets the Type of the current instance.

(Inherited from Object)
MemberwiseClone()

Creates a shallow copy of the current Object.

(Inherited from Object)
ToString()

Returns a string that represents the current object.

(Inherited from Object)

Applies to

Produk Versi
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.3, 1.4, 1.6, 2.0, 2.1