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 ...
随机推荐
- Android按键添加和处理的方案
Android按键添加和处理的方案 版本号 说明 作者 日期 1.0 Android按键添加和处理的方案 Sky Wang 2013/06/18 需求:Android机器上有个W ...
- U盘装win7系统
首先在互联网下载UltraISO光盘映像文件制作/编辑/格式转换工具,(当然还有其它如WinISO.WinImage.Daemon Tools等)然后在准备一个4GB容量以上(含4GB)的优盘或者移动 ...
- android studio build.gradle中 project.ANDROID_BUILD_SDK_VERSION
1.メニューの [File] -> [Import Module]2.Source directory に先ほど解凍したディレクトリを指定3.「facebook」 を選択した状態に Finish ...
- Tomcat服务器集群与负载均衡实现
一.前言 在单一的服务器上执行WEB应用程序有一些重大的问题,当网站成功建成并开始接受大量请求时,单一服务器终究无法满足需要处理的负荷量,所以就有点显得有 点力不从心了.另外一个常见的问题是会产生单点 ...
- 解决javamail ssl 测试unable to find valid certification path to requested target
运行 java InstallCert smtp.interdrp.com:465 得到jssecacerts文件后复制到jdk1.6.0_14\jre\lib\security目录 然后再发送邮件就 ...
- [转]如何将PHP作为Shell脚本语言使用
From : http://www.linuxfly.org/post/559/ 我们都知道,PHP是一种非常好的动态网页开发语言(速度飞快,开发周期短……).但是只有很少数的人意识到PHP也可以很好 ...
- Spring Data JPA 实现多表关联查询
本文地址:https://liuyanzhao.com/6978.html 最近抽出时间来做博客,数据库操作使用的是 JPA,相对比 Mybatis 而言,JPA 单表操作非常方便,增删改查都已经写好 ...
- jquery each循环遍历完再执行的方法 因为each是异步的 所以要加计数器.
query each循环遍历完再执行的方法因为each是异步的 所以要加计数器.var eachcount=0;$(“.emptytip”).each(function(){ eachcount++c ...
- POJ 1719 Shooting Contest(二分图匹配)
POJ 1719 Shooting Contest id=1719" target="_blank" style="">题目链接 题意:给定一个 ...
- OpenWRT - WEB界面开发思路和基本方法
想要对OpenWRT的WEB界面(*下称界面)进行修改.修改的目标是: 1.修改页面的样式,设计为企业的风格(stylesheet) 2.新建自己的功能,实现 访问页面后,用户就可以对配置文件(也就是 ...