Mina.Net实现的UDP多路广播

主要用于未确定主机地址的情况下,可以使用多路广播和服务端通信,下面是官方提供的DEMO。

using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
using Mina.Core.Session;
using Mina.Filter.Codec;
using Mina.Filter.Codec.TextLine;
using Mina.Transport.Socket;

namespace MulticastUDP
{
    /// <summary>
    /// UDP Multicast
    /// 
    /// See http://msdn.microsoft.com/en-us/library/system.net.sockets.multicastoption%28v=vs.110%29.aspx
    /// </summary>
    class Program
    {
        static IPAddress mcastAddress;
        static int mcastPort;

        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");

            mcastAddress = IPAddress.Parse("224.168.100.1");
            mcastPort = 18287;

            StartMulticastAcceptor();
            StartMulticastConnector();

            Console.ReadLine();
        }

        static void StartMulticastAcceptor()
        {
            IPAddress localIPAddr = IPAddress.Any;
            AsyncDatagramAcceptor acceptor = new AsyncDatagramAcceptor();

            acceptor.FilterChain.AddLast("codec", new ProtocolCodecFilter(new TextLineCodecFactory(Encoding.UTF8)));

            // 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 client.
            MulticastOption mcastOption = new MulticastOption(mcastAddress, localIPAddr);
            acceptor.SessionConfig.MulticastOption = mcastOption;

            acceptor.SessionOpened += (s, e) =>
            {
                Console.WriteLine("Opened: {0}", e.Session.RemoteEndPoint);
            };
            acceptor.MessageReceived += (s, e) =>
            {
                Console.WriteLine("Received from {0}: {1}", e.Session.RemoteEndPoint, e.Message);
            };

            acceptor.Bind(new IPEndPoint(localIPAddr, mcastPort));

            Console.WriteLine("Acceptor: current multicast group is: " + mcastOption.Group);
            Console.WriteLine("Acceptor: current multicast local address is: " + mcastOption.LocalAddress);
            Console.WriteLine("Waiting for multicast packets.......");
        }

        static void StartMulticastConnector()
        {
            IPAddress localIPAddr = IPAddress.Any;
            IPEndPoint mcastEP = new IPEndPoint(mcastAddress, mcastPort);
            AsyncDatagramConnector connector = new AsyncDatagramConnector();

            connector.FilterChain.AddLast("codec", new ProtocolCodecFilter(new TextLineCodecFactory(Encoding.UTF8)));

            // Set the local IP address used by the listener and the sender to 
            // exchange multicast messages. 
            connector.DefaultLocalEndPoint = new IPEndPoint(localIPAddr, 0);

            // 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 = new MulticastOption(mcastAddress, localIPAddr);
            connector.SessionConfig.MulticastOption = mcastOption;

            // Call Connect() to force binding to the local IP address,
            // and get the associated multicast session.
            IoSession session = connector.Connect(mcastEP).Await().Session;

            // Send multicast packets to the multicast endpoint.
            session.Write("hello 1", mcastEP);
            session.Write("hello 2", mcastEP);
            session.Write("hello 3", mcastEP);
        }
    }
}

 

转载于:https://www.cnblogs.com/songxingzhu/p/5445983.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值