当前位置: 首页>>代码示例>>C#>>正文


C# MulticastOption类代码示例

本文整理汇总了C#中System.Net.Sockets.MulticastOption的典型用法代码示例。如果您正苦于以下问题:C# MulticastOption类的具体用法?C# MulticastOption怎么用?C# MulticastOption使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。


MulticastOption类属于System.Net.Sockets命名空间,在下文中一共展示了MulticastOption类的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: MulticastOptionProperties

//引入命名空间
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 mcastAddress;
    private static int mcastPort;
    private static Socket mcastSocket;
    private static MulticastOption mcastOption;


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


    private static void StartMulticast() 
    {
    
      try 
      {
        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, mcastPort);

        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 (Exception e) 
      {
        Console.WriteLine(e.ToString());
      }
    }

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

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

          mcastSocket.ReceiveFrom(bytes, ref remoteEP);

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

        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.
      mcastAddress = IPAddress.Parse("224.168.100.2");
      mcastPort = 11000;
    
      // Start a multicast group.
      StartMulticast();
      
      // Display MulticastOption properties.
      MulticastOptionProperties();
      
      // Receive broadcast messages.
      ReceiveBroadcastMessages();
    }
  }
}
开发者ID:.NET开发者,项目名称:System.Net.Sockets,代码行数:124,代码来源:MulticastOption

示例2: JoinMulticastGroup

//引入命名空间
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 TestMulticastOption
  {

    static IPAddress mcastAddress;
    static int mcastPort;
    static Socket mcastSocket;
   
    static void 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("\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.
        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(mcastAddress, localIPAddr);
        
        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(mcastAddress,mcastPort);
        mcastSocket.SendTo(ASCIIEncoding.ASCII.GetBytes(message), endPoint);			
        Console.WriteLine("Multicast data sent.....");
      }
      catch (Exception e)
      {
        Console.WriteLine("\n" + e.ToString());
      }

      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.
      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.");
    }
  }
}
开发者ID:.NET开发者,项目名称:System.Net.Sockets,代码行数:94,代码来源:MulticastOption


注:本文中的System.Net.Sockets.MulticastOption类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。