主要用于未确定主机地址的情况下,可以使用多路广播和服务端通信,下面是官方提供的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 = ; 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, ); // 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);
}
}
}

Mina.Net实现的UDP多路广播的更多相关文章

  1. iOS 利用Socket UDP协议广播机制的实现

    1.前言 什么是UDP协议广播机制? 举一个例. 比如在一群人群中,一个人要找张三,于是你向人群里大喊一声(广播):"谁是张三" 假设它是张三,它就会回应你.在网络中也是一样的. ...

  2. 第14章 UDP编程(3)_利用UDP实现广播功能

    3. 广播的介绍 (1)广播 ①广播实现一对多的通信,如QQ群 ②它通过向广播地址发送数据报文实现的 (2)SO_BROADCAST选项 ①SO_BROADCAST选项控制着UDP套接字是否能发送广播 ...

  3. iOS- 移动端Socket UDP协议广播机制的实现

    1.前言    什么是UDP协议广播机制?      举一个例, 例如在一群人群中,一个人要找张三,于是你向人群里大喊一声(广播):“谁是张三”   如果它是张三,它就会回应你,在网络中也是一样的. ...

  4. C# 委托链、多路广播委托

    委托链.多路广播委托:也就是把多个委托链接在一起,我们把链接了多个方法的委托称为委托链或多路广播委托 例: class HelloWorld { //定义委托类型 delegate void Dele ...

  5. uip UDP server广播模式(client能够随意port,而且主动向client发送数据)

    眼下移植uip,发现UDP server模式下,必须指定本地port以及clientport,否则仅仅能讲clientport设置为0,才干接收随意port的数据,可是无法发送数据,由于此时clien ...

  6. Mina传输大数组,多路解码,粘包问题的处理

    我的实际情况: 1,传递的业务数据种类很多,这就决定了我们要用多路解码器,MINA的中文手册提供的是DemuxingProtocolCodecFactory; 2,,有的数据长度达到8K,网上有资料说 ...

  7. libnet发包例子(tcp udp arp广播)

    #include <libnet.h> int main() { libnet_t *handle; /* Libnet句柄 */ int packet_size; /* 构造的数据包大小 ...

  8. c#和UDP SOCKET广播

    server:  Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Dgram,ProtocolType.Udp); // ...

  9. 五十八、linux 编程——UDP 编程 广播

    58.1 广播介绍 58.1.1 介绍 广播实现一对多的通讯 它通过向广播地址发送数据报文实现的 58.1.2 套接字选项 套接字选项用于修饰套接字以及其底层通讯协议的各种行为.函数 setsocko ...

随机推荐

  1. 咏南中间件支持DELPHI6及以上版本开发的客户端

    咏南中间件支持DELPHI6及以上版本开发的客户端 下面是测试用DELPHI7开发的客户端,调用DELPHI10.3.1开发的中间件,完全兼容. 这样,有利于维护原来用D7开发的老程序. 如上所述,只 ...

  2. activity_main.xml 要用 Android Common XML Editor打开,双击的方式直接跳转到浏览器了

  3. 完全定制UITabBarViewController

    完全定制UITabBarViewController 效果   源码 https://github.com/YouXianMing/iOS-Project-Examples 中的 TotalCusto ...

  4. node.js使用mysql模块的坑

      之前用node.js写的订餐系统,很容易挂掉,一直也没想去解决它.今天看了一下,试了试,原因是在连接数据库的时候没有对error事件进行处理,导致程序一直挂在那里,需要重启服务才能正常使用.   ...

  5. Java比较两个Date日期的大小

    import java.util.*; import java.text.ParseException; import java.text.SimpleDateFormat; class Test{ ...

  6. MAC 上的 Live Writer : ecto

    ecto 在这里: http://illuminex.com/ecto/ 它是一款 MAC 上的 live writer,对我来说,没有 writer 之类的工具,我就宁可不写博客。 测试插入图片 测 ...

  7. sugar crm

    百度百科:http://baike.baidu.com/link?url=7SnriwrF-4LcRfXctBbZjLc-UEUqWl3b0YR004pGFk4SJ1qMU9TMj37yFmHRsUS ...

  8. Asp.netMVC中Html.Partial,RenderPartial,Action,RenderAction区别和用法

    https://www.2cto.com/kf/201702/602449.html Partial 和RenderPartial: 这两个的性质都是一样, 只指把一个个View给镶入进来, 只是回传 ...

  9. 迪米特法则(Law Of Demeter)

    定义:一个对象应该对其他对象保持最少的了解. 问题由来:类与类之间的关系越密切,耦合度越大,当一个类发生改变时,对另一个类的影响也越大. 解决方案:尽量降低类与类之间的耦合. 自从我们接触编程开始,就 ...

  10. 在Linux中查看正在运行哪些process,杀掉一批名字相同的process

    列出全部进程: ps -A 杀掉所有名为netmist的进程 killall netmist 参考资料 ============ https://www.howtogeek.com/107217/ho ...