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 = ; 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多路广播的更多相关文章
- iOS 利用Socket UDP协议广播机制的实现
1.前言 什么是UDP协议广播机制? 举一个例. 比如在一群人群中,一个人要找张三,于是你向人群里大喊一声(广播):"谁是张三" 假设它是张三,它就会回应你.在网络中也是一样的. ...
- 第14章 UDP编程(3)_利用UDP实现广播功能
3. 广播的介绍 (1)广播 ①广播实现一对多的通信,如QQ群 ②它通过向广播地址发送数据报文实现的 (2)SO_BROADCAST选项 ①SO_BROADCAST选项控制着UDP套接字是否能发送广播 ...
- iOS- 移动端Socket UDP协议广播机制的实现
1.前言 什么是UDP协议广播机制? 举一个例, 例如在一群人群中,一个人要找张三,于是你向人群里大喊一声(广播):“谁是张三” 如果它是张三,它就会回应你,在网络中也是一样的. ...
- C# 委托链、多路广播委托
委托链.多路广播委托:也就是把多个委托链接在一起,我们把链接了多个方法的委托称为委托链或多路广播委托 例: class HelloWorld { //定义委托类型 delegate void Dele ...
- uip UDP server广播模式(client能够随意port,而且主动向client发送数据)
眼下移植uip,发现UDP server模式下,必须指定本地port以及clientport,否则仅仅能讲clientport设置为0,才干接收随意port的数据,可是无法发送数据,由于此时clien ...
- Mina传输大数组,多路解码,粘包问题的处理
我的实际情况: 1,传递的业务数据种类很多,这就决定了我们要用多路解码器,MINA的中文手册提供的是DemuxingProtocolCodecFactory; 2,,有的数据长度达到8K,网上有资料说 ...
- libnet发包例子(tcp udp arp广播)
#include <libnet.h> int main() { libnet_t *handle; /* Libnet句柄 */ int packet_size; /* 构造的数据包大小 ...
- c#和UDP SOCKET广播
server: Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Dgram,ProtocolType.Udp); // ...
- 五十八、linux 编程——UDP 编程 广播
58.1 广播介绍 58.1.1 介绍 广播实现一对多的通讯 它通过向广播地址发送数据报文实现的 58.1.2 套接字选项 套接字选项用于修饰套接字以及其底层通讯协议的各种行为.函数 setsocko ...
随机推荐
- iOS 7.1 系统可以设置 button shapes,此功能可让按钮多一条下滑线
IniOS 7, Apple completely revamped the user interface to give it a fresh and modern look. One of the ...
- 实现iOS长时间后台的两种方法:Audiosession和VOIP
http://www.cocoachina.com/applenews/devnews/2012/1212/5313.html 我们知道iOS开启后台任务后可以获得最多600秒的执行时间,而一些需要在 ...
- C#编程(六十二)---------LINQ标准的查询操作符
LINQ标准的查询操作符 首先我们来看一下LINQ的操作符,可根据查询操作符的操作”类型”进行分类,如把它们分成投影,限制,排序,联接,分组,串联,聚合,集合,生成,转换,元素,相等,量词,分割等. ...
- 【Devops】【docker】【CI/CD】2.docker启动jenkins环境+安装必要的插件
[注意:]jenkins的docker镜像,需要从官网进入直接获取,其他地方获取到的docker镜像,可能因为Jenkins版本过低,导致后续插件安装失败等问题!!! ================ ...
- Windows:C:\Windows\System32\drivers\etc\hosts
原文地址:http://zhumeng8337797.blog.163.com/blog/static/100768914201001983953188/. host是一个没有扩展名的系统文件,可以用 ...
- 并查集(Union-Find) 应用举例 --- 基础篇
本文是作为上一篇文章 <并查集算法原理和改进> 的后续,焦点主要集中在一些并查集的应用上.材料主要是取自POJ,HDOJ上的一些算法练习题. 首先还是回顾和总结一下关于并查集的几个关键点: ...
- SharePoint Online 创建网站集
前言 本文介绍如何在Office 365中创建SharePoint网站集. 正文 通过登录地址登录到Office 365环境中,我们可以在左上角的按钮中点开,进入管理员,也可以直接在页面中点击管理: ...
- 限制EditText必须输入中文的方法
给EditText做限制时,我们想要输入的字符串必须都是中文,不出现任何其他字符,下面的类可以很好的实现这个要求. InputLenLimit.java package com.kale.button ...
- ClipDrawable属性介绍
ClipDrawable代表从其它位图上截取一个"图片片段",XML中的根元素为<clip.../>,截取的方向由clipOrientation控制 <?xml ...
- Orchard之生成新模板
一:启用 Code Generation 进入后台, Modules –> Developer Enable 之. 二:生成模版 首先,进入 Orchard 命令行 在 CMD 下到达解决 ...