当前位置: 首页 > news >正文

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

 

相关文章:

  • 向fedora添加rpmfusion源
  • Provisioning Services 7.8 入门系列教程之二 基础环境安装
  • iOS开发UI篇—UITableviewcell的性能优化和缓存机制
  • GlobalSign 增强型(EV) SSL 证书
  • 执行搜索 《第三篇》
  • Grovvy Step byStep Examples
  • oracle通过DBlink连接mysql(MariaDB)
  • Compile FreeCAD on Windows
  • Elasticsearch-DSL(highlight) 004
  • linux-selinux安全防护,加密解密
  • Lua 笔记--编译、执行、错误与协同程序
  • 解决input file按钮要点击两次才弹出选择文件窗口
  • java-NIO
  • 转:对分库分表的一些想法
  • Web 图形验证码的校验1
  • [PHP内核探索]PHP中的哈希表
  • 「前端」从UglifyJSPlugin强制开启css压缩探究webpack插件运行机制
  • 2017 前端面试准备 - 收藏集 - 掘金
  • ABAP的include关键字,Java的import, C的include和C4C ABSL 的import比较
  • axios 和 cookie 的那些事
  • cookie和session
  • gulp 教程
  • JavaScript HTML DOM
  • Java编程基础24——递归练习
  • java中的hashCode
  • LeetCode刷题——29. Divide Two Integers(Part 1靠自己)
  • Linux中的硬链接与软链接
  • mongo索引构建
  • MySQL的数据类型
  • Python语法速览与机器学习开发环境搭建
  • React as a UI Runtime(五、列表)
  • Vue小说阅读器(仿追书神器)
  • 阿里云前端周刊 - 第 26 期
  • 少走弯路,给Java 1~5 年程序员的建议
  • 为什么要用IPython/Jupyter?
  • 小程序01:wepy框架整合iview webapp UI
  • 赢得Docker挑战最佳实践
  • 走向全栈之MongoDB的使用
  • 如何用纯 CSS 创作一个菱形 loader 动画
  • 我们雇佣了一只大猴子...
  • ​ 全球云科技基础设施:亚马逊云科技的海外服务器网络如何演进
  • !!Dom4j 学习笔记
  • #NOIP 2014# day.1 T3 飞扬的小鸟 bird
  • (27)4.8 习题课
  • (51单片机)第五章-A/D和D/A工作原理-A/D
  • (Demo分享)利用原生JavaScript-随机数-实现做一个烟花案例
  • (附源码)spring boot车辆管理系统 毕业设计 031034
  • (附源码)ssm户外用品商城 毕业设计 112346
  • (黑马出品_高级篇_01)SpringCloud+RabbitMQ+Docker+Redis+搜索+分布式
  • (全注解开发)学习Spring-MVC的第三天
  • (心得)获取一个数二进制序列中所有的偶数位和奇数位, 分别输出二进制序列。
  • (一)Thymeleaf用法——Thymeleaf简介
  • (转)3D模板阴影原理
  • (转)树状数组
  • ******之网络***——物理***