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 ...
随机推荐
- 详解Java Spring各种依赖注入注解的区别
注解注入顾名思义就是通过注解来实现注入,Spring和注入相关的常见注解有Autowired.Resource.Qualifier.Service.Controller.Repository.Comp ...
- FEC详解三
转自:http://blog.csdn.net/Stone_OverLooking/article/details/77752076 继续上文讲解: 3) 标准的RTP头结构如下所示: 其中第一个字节 ...
- Office Web Apps 错误日志
前言 最近一直在用Office Web Apps,使用过程会有各种各样的错误,众所周知,sharepoint的错误都在15/Logs下面保存错误日志,那么OWA呢? 经过查找,发现Office Web ...
- SharePoint Online 创建列表库
前言 本文介绍如何在Office 365中创建列表库,以及列表库的一些基本设置. 正文 通过登录地址登录到Office 365的SharePoint Online站点中,我们可以在右上角的设置菜单中, ...
- linux syslog详解 转
分三部分 一.syslog协议介绍 二.syslog函数 三.linux syslog配置 一.syslog协议介绍 1.介绍 在Unix类操作系统上,syslog广泛应用于系统日志.syslog ...
- 行尸走肉第一季/全集The Walking Dead迅雷下载
本季The Walking Dead 1 第一季(2010)看点:<行尸走肉>讲述了警察瑞克在一次执法行动中因中弹负伤而不省人事,当他从昏迷中苏醒后却惊讶地发现,这个世界已然天翻地覆.周遭 ...
- jquery实现相同事件名称,不同命名空间的调用方法
<html xmlns="http://www.w3.org/1999/xhtml"> <head> <title></title> ...
- maven与jdk版本不一致报:Unsupported major.minor version 51.0
I recently uninstalled Java 8, to use Java 6 as I want my code/creations to be usable by more people ...
- eclipse.ini 内存设置
Ubuntu 系统下,Eclipse 配置文件: vi ~/eclipse/eclipse.ini -vmargs -Xms128M -Xmx512M -XX:PermSize=64M -XX:Max ...
- 【deep learning学习笔记】注释yusugomori的DA代码 --- dA.h
DA就是“Denoising Autoencoders”的缩写.继续给yusugomori做注释,边注释边学习.看了一些DA的材料,基本上都在前面“转载”了.学习中间总有个疑问:DA和RBM到底啥区别 ...